The use of java anonymous objects


The information in this article comes from Shang Silicon Valley

Use of anonymous objects

package come.atguigu.java;

public class InstanceTest {
    
    
	public static void main(String[] args){
    
    
		Phone p = new Phone();		
		new Phone().price = 1999;
		new Phone().showPrice();

	}
}

class Phone{
    
    
	
	double price;
	
	public void sendMessage(){
    
    
		System.out.println("发邮件");
		
	}
	
	public void playGame(){
    
    
		System.out.println("打游戏");
	}
	
	public void showPrice(){
    
    
		System.out.println("价格为:" + price);
	}
}

The anonymous object does not specify a variable to take the address of the anonymous object, so the anonymous object directly opens up a space in the heap space.Although it also has an address, there is no variable in the stack space to point to it, so the anonymous object can only be used once.

note

new Phone().price = 1999;
new Phone().showPrice();

The result of its operation is

The price is: 0.0

Because the new is twice here, two objects are actually created, and the attribute of one object is assigned the value 1999.; The attribute of the other object is still the default value of 0.0, because the anonymous object can only be used once, so the meaning of this usage is not Too big, generally used to output sentences.

General usage of anonymous objects

package come.atguigu.java;

public class InstanceTest {
    
    
	public static void main(String[] args){
    
    
		Phone p = new Phone();
//		p=null;
//		System.out.println(p);
		
		new Phone().price = 1999;
		new Phone().showPrice();
		PhoneMall mall = new PhoneMall();
		mall.show(new Phone());
		System.out.println(mall);
	}
}

class PhoneMall{
    
    
	public void show(Phone phone){
    
    
		phone.sendMessage();
		phone.playGame();
	}

}

class Phone{
    
    
	
	double price;
	
	public void sendMessage(){
    
    
		System.out.println("发邮件");
		
	}
	
	public void playGame(){
    
    
		System.out.println("打游戏");
	}
	
	public void showPrice(){
    
    
		System.out.println("价格为:" + price);
	}
}

Create a PhoneMall class, the parameter of this class method is a custom type of Phone.

PhoneMall mall = new PhoneMall();
mall.show(new Phone());

The form of new Phone() here is an anonymous object, but when the actual parameter is passed to the formal parameter

class PhoneMall{
    
    
	public void show(Phone phone){
    
    
		phone.sendMessage();
		phone.playGame();
	}

}

The variable phone in the formal parameter starts to point to the address of the anonymous object in the heap space, so it has the variable phone in the stack space pointing to the address in the heap space, which can be used more than once.

Address value output

Add:

Phone p = new Phone();
//		p=null;
System.out.println(p);

If p=null is not added, the output is the address value. If p=null is
added , the output is null and the output result is

come.atguigu.java.Phone@15db9742

ps: If an object array is created, the default value in the array is null, and the object address is only after assignment

Guess you like

Origin blog.csdn.net/Meloneating/article/details/112981638