Article Directory
Classification of variables
According to data type
According to the position declared in the class
Understand that everything is an object
- In the Java language category: we encapsulate functions, structures, etc. into classes,Through the instantiation of the class, to call the specific function structure
>Scanner, String, etc.
>File: File
>Network Resource: URL - When it comes to the interaction of Java language with front-end HTML and back-end database,The structure of the front and back ends is reflected in classes and objects at the Java level
Memory analysis of object array
Variables of reference type can only store two types of values: null
or 地址值
(including the type of the variable)
Anonymous object
- Understanding: The object we created is not explicitly assigned to a variable name.
- Feature: Anonymous objects can only be called once
- use
实例测试:匿名对象的使用
public class InstanceTest {
public static void main(String[] args) {
Phone p = new Phone();
// p = null;
//System.out.println(p);
//匿名对象:这里只能调用一次
// new Phone().sendEmail();
// new Phone().playGame();
new Phone().price = 1999;
new Phone().showPrice();//0.0 不同于上面的Phone对象,价格默认初始化为0
//**********************************
PhoneMall mall = new PhoneMall();
// mall.show(p); //前面创建的对象
//匿名对象的使用
mall.show(new Phone()); //创建的匿名对象
}
}
手机类Phone、手机商场类PhoneMall
class PhoneMall{
public void show(Phone phone){
phone.sendEmail();
phone.playGame();
}
}
class Phone{
double price;//价格
public void sendEmail(){
System.out.println("发送邮件");
}
public void playGame(){
System.out.println("玩游戏");
}
public void showPrice(){
System.out.println("手机价格为:" + price);
}
}