This article allows you to quickly improve the understanding of the inner class

Inner classes

What is the inner class? The concept is simple point of object-oriented programming. In object-oriented programming, is another class within a class definition, the class referred to as a nested class, there are two types, i.e. static and non-static nested nested classes. Frequency static nested classes used rarely, less than basic, the most important thing is non-static nested class, also referred to as inner classes (inner). Additional inner class is a major part of JAVA language. Internal class inside a class may be in almost any location, it may be an instance variable in the same, or in the methods of, or even a part of the expression.

If the significance of the existence of a class to another class is specified, you can put this into a class inside another class. The class is defined in the internal class can be formed as internal class. A class also defined Classes B, class B is the internal type. Class B can be used as a member of the class A look at.

Our inner class from the outside is very easy to understand, is nothing more than a class within a class definition.

First, look for three definitions as internal classes:
internal class 1, a class (class external) defined directly;
2, in a method (method of the outer class) defined within the class;
3, anonymous inner classes .

Internal class features:
1, inner classes have direct access to members outside of class, including private members
2, members of the outer class to be accessed inside the class, we must create an object within the class
3, the internal class member location is internal members class
4, class local position inside the partial inner class

Members of the private inner class is modified

  • 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.
public class InnerClass2 {
    public static void main(String[] args) {
       //创建内部类对象,并执行show()
       
	/* -- Outer2.Inner2 oi = new Outer2().new Inner2();
	 * -- 报错,Inner2已经被private了
	 */

       //3,测试被private的内部类的资源能否执行!
       new Outer2().test();
    }
}

class Outer2{
    //2,如果想要访问private的内部类,可以访问外部类提供的对应方法
    public void test() {
       //访问内部类方法
      new Inner2().show();
    }    
    
    //位置在类里方法外--成员内部类
    //1,内部类可以被private修饰,但是外界无法直接创建对象了!
    private class Inner2{
       public void show() {
           System.out.println("Inner2.show()");
       }
    }
}

Modified static static inner class called inside, but we prefer to call the nested inner class. 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 create within its periphery, but static inner classes but No.

  • It is not required depends on the creation of the enclosing class.

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

public class InnerClass3 {
    public static void main(String[] args) {
       // 创建内部类对象测试show()
    
	/* -- Outer3.Inner3 oi = new Outer3().new Inner3();
	 * -- 报错,原因是Inner3是静态的内部类
	 */
	   Outer3.Inner3 oi = new Outer3.Inner3();//Outer3.Inner3通过类名.调用类中的静态资源
       oi.show();    
       Outer3.Inner3.show2();//调用静态内部类里的静态方法
    }
}

class Outer3{    
    //1,内部类被static修饰--随着类的加载而加载,会造成内存资源浪费,并不常用!
    static class Inner3{ 
       public void show() {
           System.out.println("Inner3.show()");
       }
       static public void show2() {
           System.out.println("Inner3.show2()");
       }
    }
}

Local inner classes : There is a inner class that is nested in the method and role in, for the use of this class are primarily used to solve more complex problems, want to create a class to assist our solutions, to then do not want this class is publicly available, so we had a local inner class, as a member of the local inner classes and inner classes are compiled, but its scope has changed, it can only be in the method and property used, the properties and the method will fail.

Anonymous inner classes belong to a local inner classes, and there is no internal name of the class.

public class InnerClass5 {
    public static void main(String[] args) {
       new Hello() {// 匿名对象,本身接口不能new,这里new Hello()匿名对象,就相当于Hello接口的实现类
           // 匿名内部类
           @Override
           public void save() {
           System.out.println("save()..");
           } 
           @Override
           public void update() {
              System.out.println("update()..");
           }
       }.update();// 触发指定的方法
       new Hello2() {//抽象类的匿名内部类
           @Override
           public void show() {  }
       }.show();       
       new Animal() {//普通类的匿名内部类
           @Override
           public void eat() {   }
       };
    }
}

//创建匿名对象+匿名内部类测试
class Animal{
    public void eat() {}
}

abstract class Hello2 {
    abstract public void show();
    public void delete() {   }
}

// 定义接口
interface Hello {
    void save();
    void update();
}
观察上面的代码,我们看清几个地方:
	 -- 匿名内部类是没有访问修饰符的。
     -- new 匿名内部类,这个类首先是要存在的。如果我们将那个InnerClass接口注释掉,就会出现编译出错。
     -- 匿名内部类是没有构造方法的。因为它连名字都没有何来构造方法。

Summary: Why do we want to use it inside the class? Read "Think in java" should know, including this sentence: "Java programming in the internal most appealing reason: Each inner class can independently inherit an implementation (interface), so regardless of whether the enclosing class inherits an implementation (interface), for inner classes are not affected. "can even be said that the interface only solves part of the problem, while the inner class so that multiple inheritance ways and means to make solutions more complete.

Published 36 original articles · won praise 13 · views 1056

Guess you like

Origin blog.csdn.net/weixin_44598691/article/details/104971473