Dark Horse program meta-internal class

Source: Internet
Author: User

Http://edu.csdn.net/heima Android Training
Http://edu.csdn.net/heima; Java training is coming to communicate with you!

I. Internal class
1
, Internal class:A class is stored in another class, which is an internal class. The internal class name is affiliated.
2. Internal features:
A. The internal class can directly access members in the external class.
Because all internal classes hold a reference to an external class, the external class name is. This.
B. To access members of an internal class, an external class must create an internal class object.
3. When should I define an internal class?
A class is used to describe a thing. If there is a specific thing in a thing, and the internal thing accesses the content of the thing.
In this case, the internal thing is described by class. This class is an internal class.
4. Internal class modifier:

A. Permission modifier.

Default or total.


You can directly access the internal class, external class name. Internal Class Object. New
Internal class object.

When an internal class is defined as a member of an external class, the modifier of the member can be used.

How to call the content in the internal class.

B. Static modifier.

The internal class is statically modified and has access restrictions. Only static members in the external class can be accessed.

After the internal class is static, it will be loaded with the external class, and you can directly new the internal class object.

To call non-static members of an internal class, you must create an internal class object.

Access static members in the internal class without objects.

Note: If a static member is defined in an internal class, the internal class must be statically modified.

Remember: Internal classes can define these modifiers only when they define the member locations of external classes.

C. The internal class can also define the location of local variables in the external class.


The internal class definition does not allow access to local variables at a local location.


Only Local constants modified by final can be accessed.
5The internal class is defined locally:When an internal class is defined as a local position of an external class, it can only access local constants modified by final.

6. What is an anonymous internal class?

An anonymous internal class is actually an anonymous subclass object. This object defines members internally at the end.

That is to say, it is an object with member content.
7. Anonymous internal class
A. Prerequisites for defining anonymous internal classes:
Premise: the internal class must inherit or implement an external class or interface.
B. Anonymous internal class: it is actually a subclass anonymous subclass object.
8. Notes for anonymous internal use:
A. objects must be referenced when an anonymous internal class wants to call multiple methods.
B. if you use a parent class reference to receive subclass objects, you can only use methods in the parent class.

9. Why define internal classes?

A class is used to describe a thing. If there is a specific thing in a thing, and the internal thing accesses the content of the thing. In this case, the description of this internal class also needs to be used. This class is an internal class.

Sample Code:

New demo () // demo d = new demo ()

{

Void show ()

{

System. Out. println ("show run..." + num );

}

} Show (); // D. show;

10. When should I use an anonymous internal class?

Use Cases of anonymous internal classes:

When there are no more than three methods in an interface, you can use an anonymous internal class as a function parameter to pass the parameter. This simplifies the format.

However, when there are many interface methods, do not use anonymous internal classes, affecting readability.

Http://edu.csdn.net/heima Android Training
Http://edu.csdn.net/heima; Java training is coming to communicate with you! See http://edu.csdn.net/heima for details

 

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.