Java Review (XV object-oriented ---- inner class)


Most of the time, the class is defined as a separate program unit. In some cases, they will be placed inside a class defined in another class, the other classes defined in the interior of the class is called inner classes (in some places, also known as nested classes), containing the class inner class also known as external classes (in some places, also known as the host class). The introduction of JDK 1.1 Java inner classes, inner classes have the following main role.

  • Internal class provides better encapsulation, can be hidden within the outer inner class of the class, the class does not allow the same type of access to other package.
  • Internal class members can directly access the private data outside the class, because the internal classes are class members as the outside thereof, can visit each other between members of the same class. But outside the class can not access the internal implementation details of the class, such as members of variables within the class.
  • Anonymous inner classes suitable for creating those classes need to use only once.

Defines an inner class is very simple, as long as a class in another class can be defined inside - can be anywhere in the class.

Members of the inner class


Members of the inner class instance

class Circle {
    private double radius = 0;
    public static int count =1;
    public Circle(double radius) {
        this.radius = radius;
    }
     
    class Draw {     //内部类
        public void drawSahpe() {
            System.out.println(radius);  //外部类的private成员
            System.out.println(count);   //外部类的静态成员
        }
    }
}

Draw class looks like a member of the class of Circle, Circle called external classes. Internal class members unconditional access to all members of the class properties and the outer member methods (including private members and static members). This is because non-static inner class object, the preservation of an external parasitic class of objects it references.

Memory schematic nonstatic inner class object reference external object reserved

Here Insert Picture Description

Note, however, that when members of the internal and external class has the same name as the class member variables or methods, hidden phenomenon occurs, the default access inside the case is a member of the class. If you want to access members of the same name outside the class, you need to be accessed in the following form:

外部类.this.成员变量
外部类.this.成员方法

External want to visit members of the class members within the class is limited. If you want to access the members of the members of the inner class, you must first create an object inside a class member outside the class, and then by pointing to this object reference to access:
External access to internal class instance of class members
class Circle {
    private double radius = 0;
 
    public Circle(double radius) {
        this.radius = radius;
        getDrawInstance().drawSahpe();   //必须先创建成员内部类的对象,再进行访问
    }
     
    private Draw getDrawInstance() {
        return new Draw();
    }
     
    class Draw {     //内部类
        public void drawSahpe() {
            System.out.println(radius);  //外部类的private成员
        }
    }
}

Members of the inner class is attached to the outer class exists, that is to say, if you want to create an object inside the class members, provided that there must be an object of the outer class. General way to create an internal member class object as follows:


Create a member of the inner class object instance

public class Test {
    public static void main(String[] args)  {
        //第一种方式:
        Outter outter = new Outter();
        Outter.Inner inner = outter.new Inner();  //必须通过Outter对象来创建
         
        //第二种方式:
        Outter.Inner inner1 = outter.getInnerInstance();
    }
}
 
class Outter {
    private Inner inner = null;
    public Outter() {
         
    }
     
    public Inner getInnerInstance() {
        if(inner == null)
            inner = new Inner();
        return inner;
    }
      
    class Inner {
        public Inner() {
             
        }
    }
}

Partial inner class

Partial inner class is defined in a method or a class inside the scope, and the difference between its internal members that access the local class within the class or method is limited in the scope.

class People{
    public People() {
         
    }
}
 
class Man{
    public Man(){
         
    }
     
    public People getWoman(){
        class Woman extends People{   //局部内部类
            int age =0;
        }
        return new Woman();
    }
}

Local class can not be modified with public or private access modifier. Its scope is defined in the local block declaration class.


Anonymous inner classes

The use of local inner classes and then deep one step. If only to create an object of this class, you do not have a name. This class is called anonymous inner classes (anonymous inner class).

Anonymous inner class instance

public void start(int interval, boolean beep) {

  ActionListener listener = new ActionListener(){
    public void actionPerformed(ActionEvent event) {
    System.out.println("At the tone, the time is " + new  Date())if (beep) Toolkit.getDefaultToolkit().beep(); 
   } 
  }
  
  Timer t = new Timer(interval, listener);
   t.start()

 }

In the program, create a new object class that implements the ActionListener interface, the actionPerformed method need to achieve defined within the brackets.

Anonymous inner class syntax is typically as follows:

new SuperType(construction parameters) {
  inner class methods and data
}
  • SuperType If this is the ActionListener interface, inner class must implement this interface.
  • SuperType If it is a class, inner class will inherit this class.

About anonymous inner classes as well as the following two rules.

  • Anonymous inner classes can not be abstract class, because the system when you create an anonymous inner classes, anonymous inner class object is created immediately. So we do not allow anonymous inner class defined as an abstract class.
  • Not anonymous inner classes defined constructor. Since the class name is not anonymous inner classes, it is impossible to define a constructor, but anonymous inner classes can be defined initialization block, the constructor things may be done by way of example need to complete initialization block.

Static inner classes

Static inner class is defined in another class inside the class, just in front of the class of more than one keyword static. Static inner class need not rely on external class, the static class member properties and this is somewhat similar, and it can not use non-static member variables outside the class or method, since in the absence of external target class may be created static inner classes of objects, if allowed access to non-static members of the outer class is a contradiction, because the non-static member outside the class must be attached to specific objects.

Static inner class instance

public class Test {
    public static void main(String[] args)  {
        Outter.Inner inner = new Outter.Inner();
    }
}
 
class Outter {
    public Outter() {
         
    }
     
    static class Inner {
        public Inner() {
             
        }
    }
}

reference

[1]: https: //www.runoob.com/w3cnote/java-inner-class-intro.html
[2]: "crazy Java handouts"
[3]: "Java core technology Volume"

Published 136 original articles · won praise 36 · views 30000 +

Guess you like

Origin blog.csdn.net/sinat_40770656/article/details/102773107