java-Basics-Anonymous objects-inner classes

Source: Internet
Author: User
Tags modifier

The concept of anonymous objects

An anonymous object is a statement that creates an object, but does not assign an object address value to a variable.

 Public class person{    publicvoid  eat () {        System.out.println ();}}

Create a Normal object

Person p = new person ();

Create an anonymous object

New Person ();

Features of Anonymous objects:

L Create anonymous objects directly, with no variable names.

The new person (). Eat ()//eat method is called by a person object that has no name.

An anonymous object can only be used once if its reference variable is not specified.

New person (). Eat (); Create an anonymous object, call the Eat method

New person (). Eat (); To re-create an anonymous object by calling the Eat method again

An anonymous object can be used as a parameter received by a method, a method return value

classDemo { Public StaticPerson Getperson () {//Normal Way//Person p = new person (); //return p; //Anonymous object as method return value        return NewPerson (); }         Public Static voidmethod (person p) {}}classTest { Public Static voidMain (string[] args) {//call the Getperson method to get a person objectPerson person =Demo.getperson (); //Call method MethodsDemo.method (person); //an anonymous object is a parameter that is received as a methodDemo.method (NewPerson ()); }}
Inner class

Writing classes inside other classes can be written in the member and local locations of other classes, where classes written inside other classes are called inner classes. Other classes are also known as external classes.

In describing things, if a thing contains other things that might be contained inside, such as when describing a car, the engine is included in the car, and the engine can be described using an inner class.

L Classification of internal classes

Inner classes are divided into members inner classes and local inner classes.

When we define an inner class, it is a normal process of defining a class, and it also contains various modifiers, inheritance, and implementation relationships. All members of an external class can be accessed directly in the inner class.

member Inner class

A member inner class that defines the position of a member in an external class. Similar to member variables in a class, accessible through an external class object

L DEFINE the format

Class External Classes {

Modifier class Inner Class {

Other code

}

}

L access Mode

The external class name. Internal class name Variable name = new external class name (). New internal class name ();

L member internal Class code demo

class Body {// outer class, physical    Privatebooleantrue// Life State public class     // inner class, Heart     Public void Jump () {         System.out.println ("heart-popping")            System.out.println (//  Access external class member variable }}}

accessing internal classes

 Public Static void Main (string[] args) {    // Create inner class object    new Body ().  New  Heart ();     // calling methods in an inner class     bh.jump ();}
Local inner class

A local inner class that defines a local location within an external class method. Similar to local variables in Access methods, accessible by calling methods

L DEFINE the format

Class External Classes {

Modifier return value type method name (parameter) {

Class Inner Classes {

Other code

}

}

}

L access Mode

In the external class method, create an inner class object to access

L Local inner Class code demo

classParty {//outside class, party     Public voidPuffball () {//Balloon Blowing Method        classBall {//inner class, balloon               Public voidPuff () {System.out.println ("The balloon swelled up.");}}//Create an inner class object, call the Puff methodNewBall (). Puff ();}} accessing internal classes Public Static voidMain (string[] args) {//creating an external class objectParty p =NewParty (); //calling the Puffball method in an external classP.puffball ();}

The actual use of inner classes-anonymous inner classes

Temporarily defines a subclass of a specified type

Immediately after the definition, create the object of the subclass that you just defined.

function: An anonymous inner class is a shortcut to create a subclass object of a type.

Format:

New parent class or interface () {

To do a method rewrite

};

Code Demo:

// the parent class that already exists:  Public Abstract class person{    publicabstractvoid  eat ();} // defines and creates a subclass object of the parent class, and assigns a value to the parent class reference variable in a polymorphic way Person  New person () {    publicvoid  eat () {        SYSTEM.OUT.PRINTLN ("I Ate");}; // Call the Eat Method p.eat ();

Using an anonymous object, the subclass is defined with two steps to create a subclass object by one format at a time. Although it is a two-step process, the two steps are done together.

Anonymous inner class is also an anonymous object if you do not define a variable reference

New Person () {

public void Eat () {

SYSTEM.OUT.PRINTLN ("I ate");

}

}.eat ();

java-Basics-Anonymous objects-inner classes

Related Article

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.