Rules for inner classes

Source: Internet
Author: User

Inner class:
Define a class within another class, which is called an inner class.

Access features:
Inner classes can access external class members directly, including private members
Members in an external class can be accessed directly because the inner class holds
An external class that references an external class. this. Variable name
Instead, the external class must have an inner class object to access the inner class member.

An inner class can be modified by the private external class. An inner class is a member of an external class.

Format
1 when an inner class is defined on the member location of an external class, not a private, it can be outside the other class
Create an inner class object directly in the
Interview: External class. Internal class variable name =new external class (). Inner Class ()
2 when an inner class is in a member position, it can be decorated with member modifiers such as private
Encapsulating inner classes in the outer class
The static inner class has the static property:
When an inner class is statically decorated, only static members in the outer class can be accessed directly.
Access restrictions appear

In other external classes, how do you directly access non-static members of static inner classes?
External class. Internal class variable name =new external class (). Inner class (). Inner class method ();

In other external classes, how do you directly access static members of static inner classes?
External class. Inner class. Internal static method (); Static can be called directly from the class name.

Note: When a static member is defined in an inner class, the inner class must be static.
When a static method in an external class accesses an inner class, the inner class must also be static.

Definition principle:
When there is something inside the thing that describes the thing, the thing is described by an inner class
Because internal things are using the content of external things.

1 inner class definitions cannot be decorated with member modifiers at local time
2 members in external classes can be accessed directly because they also hold references in external classes
However, it is not possible to access the local variable it is in, only the final modified
Local variables.

Anonymous inner class:
1 The anonymous inner class is actually the shorthand format for the inner class.
2 Prerequisites for defining anonymous inner classes:
An inner class must inherit a class or implement an interface.
3 Format: New parent class or interface () {Defines the contents of a subclass}. function;
A function is a subclass of a defined class of tolerance. Only one function can be followed.
An anonymous object can be called only once.
4 In fact, an anonymous inner class is an anonymous subclass object.
And the object is a bit fat, can be understood as the object with content.
5 The method defined in the anonymous inner class should not be more than 3.

If the parent class variable name =new the parent class or interface () {Defines the contents of the subclass}
Only variable names. function () The function can only be a function of the parent class.
The subclass does not have a name so it cannot be cast.
If the parent class is abstract, it can be used more than once. The abstract subclass must be fully replicated.

Rules for inner classes

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.