Attributes (filed) and methods (method) of class members

Classification of variables

According to data type

Insert picture description here

According to the position declared in the class

Insert picture description here

Understand that everything is an object

  1. 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
  2. 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: nullor 地址值(including the type of the variable)
Insert picture description here

Anonymous object

  1. Understanding: The object we created is not explicitly assigned to a variable name.
  2. Feature: Anonymous objects can only be called once
  3. 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);
	}
}

Guess you like

Origin blog.csdn.net/AC_872767407/article/details/113446972