Internal class (nested class, built-in class) access rules, definition principles, static internal class, anonymous internal class

Source: Internet
Author: User

Internal class (nested class, built-in class) access rules, definition principles, static internal class, anonymous internal class
I. Internal Class 1. Internal class access rules:

1. The internal class can directly access members in the external class, including private. The reason for direct access to Members in the external class is that the internal class holds an external class reference in the format of: External class. this

2. to access an internal class, an external class object must be created.

2. Access format
1. When an internal class is defined as a member of an external class and is not private, it can be in other external classes.
Internal class objects can be directly created
Format:
External class. Internal class name variable name = External Class Object. Internal class object;
Outer. Inter in = new Outer (). new Inner ();
2. When the internal class is in the member position, it can be modified by the member modifier.
For example: private: encapsulate internal classes in external classes
Static: Internal classes have static features.
When the internal class is statically modified, it can only access static members in the external class, and access restrictions occur.

In other external classes, how do I directly access non-static members of static internal classes?
New Outer. Inner (). function ();
In other external classes, how do I directly access static members of static internal classes?
Outer. Inner. function ();
3. Notes:
When a static member is defined in an internal class, the internal class must be static.
When the static method of the external class accesses the internal class, the internal class must also be static
3. When to use internal classes
When describing a thing, there is something inside it. It is described by an internal class.
Because internal things use the content of external things
Class Outer {int x = 3; private void function () {System. out. println ("inner:" + Outer. this. x); // at this time, 3} void method () {System. out. println (x) ;}} class InnerClassDemo {public static void main (String [] args) {Outer. inter in = new Outer (). new Inner (); in. function ();}}
4. Internal class definition in local time
1. It cannot be modified by a member modifier.
2. You can directly access members of the external class because they also hold references to the external class,
However, you cannot access the local variables in which the object is located. You can only access the local variables modified by final.
Class Outer {int x = 3; void method (final int a) {class Inter // private cannot be used for modification, it cannot be static {final int y = 4; void function () {System. out. println (x); System. out. println (y); System. out. println (a);} new Inter (). function () ;}} class InterClassDemo3 {public static void main (String [] args) {Outer out = new outer (); out. method (7); out. method (8); // although it is modified by final, this is acceptable because it is a local variable and the stack memory is stored/* new Outer (). method (7); new Outer (). method (8 );*/}}
Ii. Anonymous internal class
1. Anonymous internal classes are actually abbreviated internal classes.
2. Prerequisites for defining anonymous internal classes
An internal class must inherit a class or implement an interface.
3. Anonymous internal class format: new parent class or interface () {defines the content of the subclass}
4. In fact, an anonymous internal class is an anonymous subclass object. And the object is a little fat.
It can also be understood as an object with content
5. It is recommended that no more than three methods are defined in the anonymous internal class.
Class AbcDemo {abstract void show ();} class Outer {int x = 3;/* class Inter extends AbcDemo {void show () {System. out. println ("method =" + x) ;}} */public void function () {// new Inter (). show (); // The following is equivalent to the new AbsDemo () // ************************************ AbsDemo subclass object {void show () {System. out. println ("x =" + x);} void abs () {System. out. println ("haha ");}}. show (); // you can also call abc ()} class InterclassDemo4 {public static void main (String [] args) {new Outer (). function ();}}

Interface Inter {public abstract void method ();}/* class Test {// complement the code by using the anonymous internal class static class Inner implements Inter {public void method () {System. out. println (method run) ;}} static Inter function () {return new Inner () ;}} */static Inter function () {return new Inter () {void method () {System. out. prinyln ("method run") ;}}; // The semicolon cannot be small. This is a return statement} class InterClassDemo5 {public static void main (String [] args) {// Test class has a static member method function () // and the return value must be an object, and it is an Inter type object // because only an INter type object can call the method Test. function (). method; // Inter in = Test. function (); // in. method (); show (new Inter () {public void method () {System. out. println ("method show run") ;}}) ;}public static void show (Inter in) {in. method ();}}



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.