Object-oriented (use of anonymous objects)

Source: Internet
Author: User

Tell a little bit of knowledge, anonymous object, Anonymous is no name, not the name is the abbreviated format of the object, has its own application scenario, but also has its own shortcomings.

New Car (). num = 5; Once it's done, it's rubbish.

New Car (). Color = "Blue"; once it's done, it's rubbish.

New Car (). Run ();//The Anonymous object invocation method has meaning, there is content in it, and the call property



Car c = new car ();
C.run ();

New Car (). run ();


Car c = new car ();
C.run ();
C.num = 4;
New Car (). run ();


*/
How to use anonymous objects one: When a method on an object is called only once, it can be done with an anonymous object, which is easier to write.
If you make more than one member call to an object, you must give the object a name.
How to use anonymous objects two: You can pass an anonymous object as an actual parameter.


All the simplification has the limitation, the ternary operator is the simplification of the ifelse, has the limitation, must have the computation result.

Car q = new car ();
Show (q);


Show (new Car);//This object has a name, a reference, no longer a garbage
}


Demand: Car workshop. To refit the car, the future car will be converted to black cars, three tires.
public static void Show (Car c)
{
C.num = 3;
C.color = "BLACK";
C.run ();
}

After the line is finished, the object does not have a reference to become garbage, involving a strong reference, in fact, involves the problem of weak references. In fact, the development really want to let the object become garbage Q=nul, specifically designated. References do not point to you anymore. In fact, in the Java write cache program, it is necessary to specify how long the object life cycle. If the virtual machine is not specified, the virtual machine does not receive the timer. When you release the object, consider strong references, soft references, weak references, and virtual references. Optimized for memory use. Later on, use Java to provide us with objects.

Object-oriented (use of anonymous objects)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.