"Java Basics" internal class detailed __java

Source: Internet
Author: User
Tags redis static class

1. Definition:
The definition of a class is placed inside the definition of another class, and the class that contains the inner class is called the outer class

2. The role of the internal class:

(1) The inner class provides a better encapsulation that hides the inner class within the outer class and does not allow other classes in the same package to access the class;
(2) The method of inner class can directly access all the data of external class, including private data;
(3) The functionality implemented by the inner class can be implemented using an external class, but it is sometimes more convenient to use an inner class;
(4) Each inner class can inherit an (interface) implementation independently, regardless of whether the external class has inherited a (interface) implementation, has no effect on the inner class;
(5) The inner class cannot be overwritten, but it can be implemented by inheritance.

3. Ordinary inner class (member inner Class)

(1) How to use:

The inner class is defined inside the outer class, which corresponds to the position of a member variable of the outer class, and the inner class can use any access control character, such as public, protected, private, etc.

Methods defined in an inner class can directly access data in an external class without being affected by an access control character

Once you have defined the member inner class, you must use an external class object to create the inner class object instead of going directly to the new internal class object.

namely: Internal class object name = External class object. New inner class ();

Cases:

Outer o = new Outer ();           Create an external class object
Inner io = o.new Inner ();        Create an inner class object
After compiling the above program, you will find that two. class⽂ files are generated; The class file for the external class is: the external class name. class, the class file corresponding to the internal classes is: External class name $ internal class name. class

(2) Note:

An external class is a member and method that cannot directly use an inner class, you can first create an object of an inner class, and then access its member variables and methods through an object of an inner class.

If the external class and the inner class have the same member variable or method, the inner class defaults to the amount or method of its member variables, and you can use the This keyword if you want to access the amount of member variables for the external class.

Cases:

public class Employee {
    private String name = ' Redis ';

    Class Mannger {
        private String name = ' Netty ';

        public void GetName () {
            String name = ' Lee ';
            SYSTEM.OUT.PRINTLN ("local variable:" + name);
            SYSTEM.OUT.PRINTLN ("Internal class variable:" + this.name);
            SYSTEM.OUT.PRINTLN ("External class variable:" + Employee1.this.name);
        }

    public static void Main (string[] args) {
        employee E = new Employee ();
        Mannger m = e.new Mannger ();
        M.getname ();
    }

Run Result:

Local variable: Lee
internal class variable: Netty
external class variable: Redis


4. Static internal class

(1) Definition: An inner class decorated by the static keyword.

(2) Features:

Static inner classes cannot directly access non-static members of external classes, but can be accessed through the new external class (). Member's Way

If the static members of the outer class are the same as the member names of the inner class, the static members of the external class can be accessed through the class name. static member; Static members of the external class

When you create an object for a static inner class, you do not need an object for the outer class, you can create the
namely: Internal class object name = new internal class ();

Cases:

public class Employee2 {
    private static String name = ' Redis ';
    Static class Mannger {
        private String name = ' Netty ';

        Public String GetName () {return
            this.name
        }}

    public static void Main (string[] args) {
        Mannger m = new Mannger ();
        System.out.println (M.getname ());
    }

Run Result:

Netty



5. Method Internal class

(1) Definition:
The inner class is defined in the method of the outer class, and the method inner class is visible only inside the method, which is only available within that method.

(2) Limitations:
Because the method inner class cannot be used outside of the method of the outer class, the method inner class cannot use the access control and the static modifier.

Cases:

public class Parcel {public
    string destination () {
        string label = ' top ';
        Class Destination {public

            void Readlabel () {
                System.out.println () internal classes. ");
            }
        }
        New destination (). Readlabel ();
        return label;
    }

    public static void Main (string[] args) {
        Parcel p = new Parcel ();
        System.out.println (P.destination ());
    }

Run Result:

Method Inner class. Top



6. Anonymous inner class

(1) Definition:
Only one object of the inner class is created, with no name.

(2) Note:

When using an anonymous inner class, we must inherit a class or implement an interface, but neither can inherit a class, or implement an interface.

A constructor cannot be defined in an anonymous inner class.

No static member variables and static methods can exist in an anonymous inner class.

The anonymous inner class is a local inner class, so all the restrictions for the local inner class are also valid for anonymous inner classes.

An anonymous inner class cannot be abstract, it must implement all the abstract methods of the inherited class or the implemented interface.

Anonymous inner classes cannot access local variables in external class methods, unless the variable is declared as a final type

Cases:

public class Bird {public
    Goose getName (int n) {return
        new Goose () {
            int number = 6 + N;
            public int GetNumber () {return number;}}
        ;
    }

    public static void Main (string[] args) {
        Bird Bird = new Bird ();
        Goose Goose = Bird.getname (3);
        System.out.println (Goose.getnumber ());
    }

Interface Goose {
    int getnumber ();
}

Output Result:   9



I talents, if there is a mistake, please point out ~
Thank you.

Related Article

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.