Road java learning: 28 inner classes (members, local, anonymous, static) and inheritance

Here Insert Picture Description


如果在类中再定义一个类,则将在类中再定义的那个类称为内部类。

A. Members of the inner class

1. The members of the inner class presentation

Inner classes in a class, you can get it in a private member variables directly within the class of the class.
Members of inner class syntax is as follows:

public class OuterClass{  // 外部类
    private class lnnerClass{  // 内部类
    // .......
    }
}

在内部类中可以随意使用外部类的成员方法以及成员变量。尽管这些类成员被修饰为private。

内部类的实例一定要绑定在外部类的实例上,如果从外部类中初始化一个内部类对象,那么内部类对象就好绑定在外部类对象上。内部类初始化方法与其他类初始化方法相同,都是使用new关键字。

public class OuterClass {
    innerClass in = new innerClass();  // 在外部类实例化内部类对象引用
    public void ouf() {
        in.inf();  // 在外部类方法中调用内部类方法
    }
    class innerClass {
        innerClass(){  // 内部类构造方法
    }
    public void inf() { // 内部类成员方法
    }
    int y = 0;   //定义内部类成员变量
    }
    public innerClass doit() { // 外部类方法,返回值为内部类引用
        //y = 4;    // 外部类不可以直接访问内部类成员变量
        in.y = 4; 
        return new innerClass(); // 返回内部类的引用
    }
    public static void main(String[] args) {
        OuterClass out = new OuterClass(); 
        // 内部类的对象实例化操作必须在外部类或外部类的非静态方法中实现
        OuterClass.innerClass in = out.doit(); 
        OuterClass.innerClass in2 = out.new innerClass(); 
        // 实例化一个内部类对象
    }
}

在实例化内部类对象时,不能在new操作符之前使用外部类名称实例化内部类对象,而是应该使用外部类的对象来创建其内部类的对象。

内部类对象会依赖于外部类对象,除非已经存在一个外部类对象,否则类中不会出现内部类对象。


2. Internal Interface Class upcast

If a permission-based private internal modifier is upcast parent class object, or is a direct upward transition interface within a program can be completely hidden inside specific implementation class. Can provide an external interface, a method declared in the interface. If the interface methods implemented within the class that implements the interface, you can define a plurality of internal class implements the same interface method in a different way, in the same general class is not a method to achieve multiple interface . this technique is often used in Swing programming, you can make a number of different events in response to a class.

interface OutInterface{ // 定义一个接口
    public void fun(); 
}
public class InterfaceInner {
    public static void main(String[] args) {
        OuterClass2 out = new OuterClass2(); // 实例化一个OuterClass22对象
        // 调用doit()方法,返回一个OutInterface接口
        OutInterface outinter = out.doit(); 
        outinter.fun(); // 调用fun()方法
    }
}
class OuterClass2{
 // 定义一个内部类实现OutInterface接口
    private class InnerClass implements OutInterface{
        InnerClass(String s){ // 内部类构造方法
            System.out.println(s); 
        }
        public void fun() { // 实现接口中的fun()方法
            System.out.println("访问内部类中的fun()方法"); 
        }
    }
    public OutInterface doit() { // 定义一个方法,返回值类型为OutInterface接口
        return new InnerClass("访问内部类构造方法"); 
    }
}

Run Results:
Here Insert Picture Description
OuterClass2 class defines a class is modified Internal permission for the private, internal class that implements the interface OutInterface, then modify doIt () method so that the method returns a OutInterface interface. Since the internal class InnerClass modification rights to private, so in addition to OuterClass2 class can access outside of the inner class, other classes can access, and access to doit () method. Since this method returns the type of an external interface, this interface can serve as an interface for external use. It includes a fun () method, this method implements this interface inherits inner class, if a class inherits the outer class, due to the internal authority not downcast inner class InnerClass, but also can not access the fun ( ) method, but it can be accessed fun interface () method. So it is inherited class subclass hides the implementation details, just write a subclass of people leaving an interface and an external class, but can also call fun () method, but the specific implementation process fun () method has It is well hidden, and this is the most basic use of inner classes.

非内部类不能被声明为private或protected访问类型。


3. Use this class to get a reference inside and outside the class keyword

If the member variable member variables defined in an external class and inner classes of the same name, you can use this keyword.

public class TheSameName{
    private int x; 
    private class Inner{
        private int x = 9; 
        public void doit(int x) {
            x++;  // 调用的是形参x
            this.x++; //调用内部类的变量x
            TheSameName.this.x++; //调用外部类的变量x
        }
    }
}

在类中,如果遇到内部类与外部类的成员变量重名的情况,可以使用this关键字进行处理。


II. Partial inner classes

Not only in the internal class can be defined in the class, the position may be defined in the local class can be defined as an internal class class method, or any scope.

interface OutInterface2{// 定义一个接口
}
class OuterClass3{
    public OutInterface2 doit(final String x){
    	// 在doit()方法中定义一个内部类
    	    class InnerClass2 implements OutInterface2{
    	        InnerClass2(String s){
    	            s = x; 
    	            System.out.println(s);
    	        }
    	    }
    	    return new InnerClass2("doit");
    }
}

III. Anonymous inner classes

class OuterClass4{
    public OutInterface2 doit(){
        return new OutInterface2(){// 声明匿名内部类
            private int i = 0; 
            public int getValue(){
                return i; 
            }
        };
    }
}

在匿名内部类定义结束后,需要加分号标识,这个分号并不是代表定义内部类结束的标识,而是代表创建OutInterface2引用表达式的标识。
匿名内部类编译以后,会产生以“外部类$序号”为名称的.class文件,序号以1~n排列,分别代表1~n个匿名内部类。


IV. Static inner classes

Before adding a modifier static inner classes, this inner class becomes a static inner classes. A static inner classes can declare static members, but can not declare static members in a non-static inner classes. Static inner classes have a maximum characteristic is non-static member can not be used outside of the class.

public class StaticinnerClass{
    static class Inner{
    // .........
    }
}

Inheritance V. inner class

Inner classes and other general category, as can be inherited, but inherited internal analogy inherit common class of complex, need to set up a special syntax to complete.

public class OutputInnerClass extends ClassA.ClassB{
    public OutputInnerClass(Class a){
        a.super();
    }
}
class CLassA{
    class ClassB{
    }
}

在某个类继承内部类时,必须硬性给予这个类一个带参数的构造方法,并且该构造方法的参数为需要继续内部类的外部类的引用,同时在构造方法体中使用a.super()语句,这样才能为继承提供必要的对象引用。


If wrong, please correct me criticism, comments are welcome.
Each text sentence: Please be assured that someone in this world really live the life you want. Please also understand that Congress had forbear setbacks you have not experienced.

Published 74 original articles · won praise 180 · views 30000 +

Guess you like

Origin blog.csdn.net/Fdog_/article/details/104747386