Interfaces, Lambda expressions internal class (internal)

Third, the internal class

Class is defined inside another class in the class, because inner class as follows:

① method within the class can access the data where the scope of the definition of the class, including private data.

② inner class can be hidden for other classes of the same package.

③ When you want to define a callback function and do not want to write a lot of code, using anonymous inner classes more convenient.

Each inner class can independently inherit an implementation (interface), regardless of whether the enclosing class have inherited (interfaces) to achieve a certain, for inner classes have no effect

1, using the internal state of the object class to access

Inner class can only access their own data fields, create data fields can also access its enclosing class object.

At compile time, the compiler will generate a default constructor for the inner class, adds a reference parameter enclosing class.

public B(A a){

   outer = a;        

}

A class is a peripheral, outer is not a keyword, just use it to explain the mechanism inside the class.

 

2, inner class basis (grammar)

public class OuterClass {

    private String name ;

    private int age;

 

    / ** ** omit getter and setter methods /

   

    public class InnerClass{

        public InnerClass(){

            name = "chenssy";

            age = 23;

        }

       

        public void display(){

            System.out.println("name:" + getName() +"   ;age:" + getAge());

        }

    }

   

    public static void main(String[] args) {

        OuterClass outerClass = new OuterClass();

        OuterClass.InnerClass innerClass = outerClass.new InnerClass();

        innerClass.display();

    }

}

Must specify the type of the object class when the internal reference ①

OuterClassName.InnerClassName

When ② to create an internal class object

OuterClass.InnerClass innerClass = outerClass.new InnerClass();

③ need to generate a reference to an external class object, use OuterClassName.this

 

Note: All internal static fields declared in the class must be final, because for each external object, will each have an internal instance of a class, if the domain is not final, it is probably not the only one.

Inner classes can not have static method

Internal compiler class is a phenomenon, regardless of the virtual machine, the compiler will translate into the inner class with a $ separate conventional external class file class and inner class names

Such as:

OuterClass enclosing class and a class called InnerClass inside, and after a successful compilation, so there will be two class files: OuterClass.class and OuterClass $ InnerClass.class

 

3, members of the inner class

Members of the inner class is the most common internal class, which is a member of the enclosing class, all properties and methods to access the peripheral members of the class so he can be unlimited, albeit private, but access to the enclosing class member properties within the class and the methods to be accessible inside class instance.

Note: ① members of the inner class can not exist any static variables and methods, ② members of the inner class attached peripherals category, only create a class to create an internal peripheral class.

public class OuterClass {

    private String str;

   

    public void outerDisplay(){

        System.out.println("outerClass...");

    }

   

    public class InnerClass{

        public void innerDisplay(){

            // use property within the peripheral

            str = "chenssy...";

            System.out.println(str);

            // use within the peripheral

            outerDisplay();

        }

    }

   

    / * Recommended getXXX () to get the inner member of the class, especially when the inner class constructor parameter None * /

    public InnerClass getInnerClass(){

        return new InnerClass();

    }

   

    public static void main(String[] args) {

        OuterClass outer = new OuterClass();

        OuterClass.InnerClass inner = outer.getInnerClass (); // can create a class internal object to call a method in the class internal

        inner.innerDisplay ();

    }

}

4, a partial inner class

Local inner classes are nested in the methods and scope, for the use of this class are primarily used to solve more complex problems, want to create a class to assist our solutions, we do not want this class is available to the public, so generated local inner classes, as the local internal classes and classes are compiled inner member, but its scope is changed, it can only be used in the methods and properties, and the properties of the method will fail.

It can not be declared with a public or private specifier.

① defined in the method in

 

public class Parcel5 {

    public Destionation destionation(String str){

        class PDestionation implements Destionation{

            private String label;

            private PDestionation(String whereTo){

                label = whereTo;

            }

            public String readLabel(){

                return label;

            }

        }

        return new PDestionation(str);

    }

   

    public static void main(String[] args) {

        Parcel5 parcel5 = new Parcel5();

        Destionation d = parcel5.destionation("chenssy");

    }

}

 

② defined in scope

public class Parcel6 {

    private void internalTracking(boolean b){

        if(b){

            class TrackingSlip {// definition of a local inner class

                private String id;

                TrackingSlip (String s) {// constructor

                    id = s;

                }

                String getSlip(){

                    return id;

                }

            }

            TrackingSlip ts = new TrackingSlip("chenssy");

            String string = ts.getSlip();

        }

    }

   

    public void track(){

        internalTracking(true);

    }

   

    public static void main(String[] args) {

        Parcel6 parcel6 = new Parcel6();

        parcel6.track();

    }

}

 

5, an anonymous inner classes

button2.addActionListener( 

                new ActionListener(){ 

                    public void actionPerformed(ActionEvent e) { 

                        System.out.println ( "You press the button II"); 

                    } 

                });

① anonymous inner classes do not have access modifiers

②new anonymous inner class that is there first

③ When using anonymous inner classes If a time parameter, the parameter to be set as final.

④ anonymous inner class has no constructor.

 

6, static inner classes

Use static modification is called static inner classes. Between the static inner classes and a non-static inner classes The biggest difference, we know that non-static inner classes after the completion of the translation will implicitly holds a reference that points to the enclosing class that created it, but static inner classes but No. Without this quote means:

① its creation does not depend on the enclosing class

② it can not use any non-static member variables and methods of the enclosing class.

③ different from the conventional inner classes, static inner class of static fields and methods.

④ inside the class declaration in the interface automatically becomes static and public classes.

 

public class OuterClass {

    private String sex;

    public static String name = "chenssy";

   

    /**

     * Static inner classes

     */

    static class InnerClass1{

        / * There may be a static member static inner classes * /

        public static String _name1 = "chenssy_static";

       

        public void display(){

            /*

             * Static inner classes can only access static member variables and methods of the enclosing class

             * Non-static member variables and methods can not access the peripheral category

             */

            System.out.println("OutClass name :" + name);

        }

    }

   

    /**

     * Non-static inner classes

     */

    class InnerClass2{

        / * Non-static inner classes can not have static members * /

        public String _name2 = "chenssy_inner";

        / * Non-static inner classes can call any member of the enclosing class, whether it is static or non-static * /

        public void display(){

            System.out.println("OuterClass name:" + name);

        }

    }

   

    public void display(){

        / * Enclosing class access static inner classes: an inner class * /.

        System.out.println(InnerClass1._name1);

        / * Static inner classes can create the instance does not need to rely on peripheral class * /

        new InnerClass1().display();

       

        / * Create a non-static inner periphery of the need to rely on class * /

        OuterClass.InnerClass2 inner2 = new OuterClass().new InnerClass2();

        / * Non-static inner classes bearing members need to use examples of non-static inner class * /

        System.out.println(inner2._name2);

        inner2.display();

    }

   

    public static void main(String[] args) {

        OuterClass outer = new OuterClass();

        outer.display();

    }

}

Published 71 original articles · won praise 42 · views 60000 +

Guess you like

Origin blog.csdn.net/dreame_life/article/details/102690878