Dark Horse programmer-java basics-internal class, dark horse programmer java Basics

Source: Internet
Author: User

Dark Horse programmer-java basics-internal class, dark horse programmer java Basics
Zookeeper

Dark Horse programmer-java basics-internal class

------ Java training, Android training, iOS training, and. Net training. We look forward to communicating with you! -------

Internal class

If Class A needs to directly access the members of Class B, and Class B needs to create an object of Class. To facilitate design and access, Class A is directly defined in Class B. You can. Class A is called an internal class. Internal classes can directly access members in external classes. To access an internal class, you must create an object for the internal class.

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 name. this
2. To access internal classes, an internal class object must be created.

Access format
1. When the internal class defines the member location of the external class and is not private, it can be in other external classes. Internal class objects can be directly created.
Format
External class name. Internal class name variable name = External Class Object. Internal class object;
Outer. Inner in = new Outer (). new Inner ();

<Span style = "font-size: 14px;"> public class InnerClassDemo {public static void main (String [] args) {// Outer out = new Outer (); // out. method (); // directly access members in the internal class. Outer. inner in = new Outer (). new Inner (); in. function () ;}} class Outer {private int x = 3; class Inner // internal class {int x = 4; void function () {int x = 6; System. out. println ("innner:" + x); System. out. println ("innner:" + this. x); System. out. println ("innner:" + Outer. this. x) ;}} void method () {Inner in = new Inner (); in. function () ;}}</span>

2,When an internal class is defined as a member location in an external class, you can use some member modifiers to modify private and static.

1: Default modifier.

Direct access to internal class format: External class name. Internal class name variable name = External Class Object. Internal class object;

Outer. Inner in = new Outer. new Inner (); // This form is rarely used.

However, this kind of application is rare, because the internal class is defined internally for encapsulation. Internal class objects are usually obtained through external class methods. In this way, you can control the internal Department objects.

2: Private modifier.

Internal classes are usually encapsulated and private, because encapsulation does not allow other programs to access them directly.

3: static modifier.

If the internal class is statically modified, it is equivalent to an external class, and there will be access limitations. Only static members in the external class can be accessed.

Note: If the internal class defines static members, the internal class must be static.

<Span style = "font-size: 14px;"> class Outer2 {private static int x = 3; static class Inner // static internal class {static void function () {System. out. println ("innner:" + x) ;}} static class Inner2 {void show () {System. out. println ("inner2 show") ;}} public static void method () {// Inner. function (); new Inner2 (). show () ;}} public class InnerClassDemo2 {public static void main (String [] args) {// TODO Auto-generated method stub // Outer 2. method (); // Outer2.Inner. function (); // new Outer2.Inner (). function (); // directly access members in the internal class. Outer2.Inner in = new Outer2.Inner (); in. function () ;}</span>

After the internal class is compiled, the file name is: "external class name $ internal class name. java"

Why can internal classes directly access members in external classes?

This is because internal references to an external class are held. The reference is the external class name. this

The internal class can be defined as the member location in the external class or the local location in the external class.

Internal class definition in local time

1, cannot be modified by the member Modifier

2. You can directly access members in the external class because they also hold references in the external class. However, you cannot access the variables in its local location. Only local variables modified by final can be accessed.

<span style="font-size:14px;">public class InnerClassDemo3 {public static void main(String[] args) {// TODO Auto-generated method stubOuter1 out = new Outer1();out.method(7);}}class Outer1 {int x = 3;void method(final int a) {final int y = 4;class Inner {void function() {System.out.println(y);}}new Inner().function();}}</span>
Anonymous internal class
1. The anonymous internal class is in short format.
2. Prerequisites for defining anonymous internal classes:
An internal class must inherit a class or implement an interface.
3. Format of the anonymous internal class: 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 be understood as an object with content.
5. It is recommended that no more than three methods are defined in the anonymous internal class.
<Span style = "font-size: 14px;"> public class InnerClassDemo4 {public static void main (String [] args) {// TODO Auto-generated method stubnew Outer4 (). function () ;}} abstract class AbsDemo {abstract void show () ;}class Outer4 {int x = 3; public void function () {AbsDemo d = new AbsDemo () {int num = 9; void show () {System. out. println ("num =" + num);} void abc () {System. out. println ("haha") ;}}; d. show (); // d. abc (); // compilation failed; }}</span>




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.