Java internal classes and anonymous classes

Source: Internet
Author: User
Tags anonymous constructor
The mention of Java internal class (Inner Class) may be unfamiliar to many people, in fact, similar concepts in C + +, that is, nested class (Nested Class), about the difference between the two and the connection, in the following will be compared. The inner class looks at the surface, is to define a class in the class (see below, the inner class can be defined in many places), but it is not so simple, at first glance the inner class seems superfluous, and its usefulness may not be so significant to the beginner, but with the deep understanding of it, You will find that the Java designer is really well-intentioned in the inner class. Learning to use internal classes is part of Mastering Java Advanced Programming, which allows you to design your program structure more gracefully. The following are described in the following ways:

First time to meet

 public interface Contents {int value ();}

Public interface Destination {String readlabel ();}
        public class Goods {Private Class Content implements Contents {private int i = 11; 
        public int value () {return i;
        } protected class Gdestination implements destination {private String label;
        Private Gdestination (String whereto) {label = Whereto; 
        Public String Readlabel () {return label;
    } Public Destination Dest (String s) {return new gdestination (s);
    Public Contents cont () {return new Content ();
        } class Testgoods {public static void main (string[] args) {goods p = new Goods ();
        Contents C = P.cont ();
    Destination D = p.dest ("Beijing"); }
} 
In this example, class content and gdestination are defined within the class goods, and each has protected and private modifiers to control the access level. Content represents the contents of goods, and gdestination represents the destination of goods. They implement two interface content and destination respectively. In the main method that follows, you use Contents C and destination D directly, and you don't even see the names of these two inner classes! In this way, the first benefit of the inner class is reflected--hiding what you don't want others to know, or encapsulation.
At the same time, we have discovered that the first way to get an inner class object outside the scope of the outside class is to create and return it using the methods of its external classes. This is done by the Cont () and Dest () methods in the previous example. So is there any other way? Of course, its grammatical format is as follows:

Outerobject=new Outerclass (constructor Parameters);

Outerclass.innerclass innerobject=outerobject.new Innerclass (constructor Parameters);

Note When you create a Non-static inner class object, you must first create the corresponding external class object. As for the reason, it leads us to the next topic-

A non-static inner class object has a reference to its external class object
To make a slight amendment to the example just now:

 public class Goods {private valuerate=2;
        Private class Content implements Contents {private int i = 11*valuerate; 
        public int value () {return i;
        } protected class Gdestination implements destination {private String label;
        Private Gdestination (String whereto) {label = Whereto; 
        Public String Readlabel () {return label;
    } Public Destination Dest (String s) {return new gdestination (s);
    Public Contents cont () {return new Content (); }} 
   
   

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.