java object-oriented - anonymous objects

 Anonymous object:

  That is not the name of the object.

Copy the code
class the Test {public 
    public static void main (String [] args) { 
        / * Car c1 = new new Car (); // create an object with a name 
        c1.run (); 
        c1.run (); 

        . new new Car () RUN (); // anonymous object calling the method 
        new Car () run (); . // anonymous object is only suitable for the method of the first call, because it will generate multiple calls to multiple objects, as with the name of the object has     
    
        // anonymous and whether the object can call the property assignment? What is the significance? 
        / * 
        anonymous can call the object properties, but does not make sense, because after calling into a garbage 
        If you need to have the assignment or use moniker 
        * / 
        new new Car (). Color = "Red "; 
        new new Car () = NUM. 8;. 
        new new Car () RUN ();. 
    } 
} 

class Car { 
    String color; // color 
    int num; // number of tires

    public void run() {
        System.out.println(color + "..." + num);
    }
}
Copy the code

Use an anonymous object:

1. When the object of the method is invoked only once, it can be reduced to an anonymous object.

As an object needs to be called Method 2 times, anonymous objects

new Car().run()

new Car().run()

This is the two objects are called run (), is not an object called a multi-method.

2. anonymous objects can be passed as actual parameters.

public static void show(Car c)
{
//......
}
show(new Car());

Memory analysis:

new Car().num =5;
new Car().color = "green";
new Car().run();

 

After an anonymous object is finished, since no further references cited's, Java's automatic garbage collection mechanism will be regarded as

  That is not the name of the object.

Copy the code
public class Test {
    public static void main(String[] args) {
        /*Car c1 = new Car();            //创建有名字的对象
        c1.run();
        c1.run();

        new Car().run();            //匿名对象调用方法
        new Car().run();            //匿名对象只适合对方法的一次调用,因为调用多次就会产生多个对象,不如用有名字的对象    
    
        //匿名对象是否可以调用属性并赋值?有什么意义?
        /*
        匿名对象可以调用属性,但是没有意义,因为调用后就变成垃圾
        如果需要赋值还是用有名字对象
        */
        new Car().color = "red";
        new Car().num = 8;
        new Car().run();
    }
}

class Car {
    String color;            //颜色
    int num;                //轮胎数

    public void run() {
        System.out.println(color + "..." + num);
    }
}
Copy the code

Use an anonymous object:

1. When the object of the method is invoked only once, it can be reduced to an anonymous object.

As an object needs to be called Method 2 times, anonymous objects

new Car().run()

new Car().run()

This is the two objects are called run (), is not an object called a multi-method.

2. anonymous objects can be passed as actual parameters.

public static void show(Car c)
{
//......
}
show(new Car());

Memory analysis:

new Car().num =5;
new Car().color = "green";
new Car().run();

 

After an anonymous object is finished, since no further references cited's, Java's automatic garbage collection mechanism will be regarded as

Guess you like

Origin www.cnblogs.com/wuqiance/p/11605012.html