[Improve] java (18) --- non-static static inner classes and inner classes

java improve] (18) - static and non-static inner inner classes

定义 Placed inside of a class of class we called inner classes.

In fact, his own inner classes from development to now mainly in two places would consider using inner classes:

1, using the static inner singleton class

2, the entity Bean Json string to time, also consider creating an inner class

Other such as the Internet, said through an internal class implements multiple inheritance, I have not used. This blog will mainly static inner classes and members of non-static inner classes inside the class , as a local inner classes and anonymous inner class here is not elaborated.

First, the concept

We can simply put inside the class as a member of the outer class, if you are static inner classes, then the class does not need external access you can create objects,

If you are a non static class category, then you belong to the category of external objects, so be sure to create an external object in order to access you.

1, non-static inner classes

Members of the inner class is the most common internal class, which is a member of the enclosing class, so it can access all member properties and methods of the enclosing class . The same members of the enclosing class can also access the properties and methods within the class.

Its main features are:

1、成员内部类中不能存在任何static的变量和方法

2、成员内部类是依附于外围类的对象,所以只有先创建了外围类对象才能够创建内部类对象

Supplementary : For not appear static field is not entirely within the internal members, if you are using the final and static while modifying a property field , and this field is the basic type or String

Type, then that can be compiled by. As for the reason you need to know the class file, wrote a related blog before I can refer to: [JVM virtual machine] (7) --- in-depth understanding of Class - a set of attributes

2, static inner classes

Use static internal modified class we call static inner classes, we need to know 只要是static修饰的类那它一定是内部类, you can not be outside of class.

There is a maximum difference between static and non-static inner classes inside the class, 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

It objects, but static inner classes do not. Without this quote means:

1、它的创建是不需要依赖于外围类的对象

2、它不能使用任何外围类的非static成员变量和方法


Second, the example

About static inner classes and non-static inner classes here to do a complete code shows

1, code demonstrates

/**
 * 外部类 OutClass
 */
public class OutClass {
    /**
     * 外部类静态属性
     */
    private static String name = "小小";
    /**
     * 外部类非静态属性
     */
    private Integer age = 3;

    /**
     * @Description: 非静态内部类
     */
    private class InnerClass {
        // TODO 非静态内部类不能声明或定义静态成员
        // private static String sex = "女";
        /**
         * 这里定义静态常量是不会报错的哦
         */
        public static final String sex = "女";
        /**
         * 可以定义 普通属性
         */
        private int flag = 0;

        /**
         * 构造函数
         */
        public InnerClass() {
            // 非静态内部类的非静态成员可以访问外部类的非静态变量和静态变量
            System.out.println("非静态类访问外部对象的name" + name);
            System.out.println("外部对象的age " + age);
        }
    }
    /**
     * @Description: 静态内部类
     */
    private static class InnerStaticClass {
        /**
         * 静态内部类可以有静态成员和非静态成员
         */
        private static String sex = "女";
        private int flag = 0;

        public InnerStaticClass() {
            System.out.println("静态类访问外部对象的name" + name);
            //静态类只能访问外部的静态成员,不能访问非静态成员
            //System.out.println("外部对象的age " + age);
        }
    }
    
    public static void main(String[] args) {
        System.out.println("==========非静态内部类调用==========");
        //new一个外部类
        OutClass outClass = new OutClass();
        // 通过外部类的对象new一个非静态的内部类
        OutClass.InnerClass innerClass = outClass.new InnerClass();
        System.out.println("==========静态内部类调用==========");
        //获取静态内部类的静态属性
        String sex = OutClass.InnerStaticClass.sex;
        //获取静态内部类的非静态属性
        OutClass.InnerStaticClass inner = new OutClass.InnerStaticClass();
        System.out.println(inner.flag);

    }
}

2, summary

Here to make a summary for the above code

  1.静态内部类可以有静态成员(方法,属性),而非静态内部类则不能有静态成员(方法,属性)。
  2.静态内部类只能够访问外部类的静态成员,而非静态内部类则可以访问外部类的所有成员(方法,属性)。
  3.实例化一个非静态的内部类的方法:
         a.先生成一个外部类对象实例
         OutClass outClass=new OutClass();
         b.通过外部类的对象实例生成内部类对象
         OutClass.InnerClass inner=outClass.new InnerClass(); 
  4.实例化一个静态内部类的方法:
         a.不依赖于外部类的实例,直接实例化内部类对象
         OutClass.InnerStaticClass inner=new OutClass.InnerStaticClass();
         b.调用内部静态类的方法或静态变量,通过类名直接调用
         OutClass.InnerStaticClass.static_sex


supplement

1, the internal action of the class

Personally, I think the following three main bar

1内部类能够提供更好的隐蔽性 . Because our inner class can be modified using private and protected, so if necessary we can guarantee that other class is

Unable to create the current internal class object. Like a singleton than our usual static inner classes.

2、通过内部类可以实现多继承

3代码可以更加整洁 . Because when we create the entity class, the class may also contain other classes, if the class will only be using the current class, then we simply create a class inside it.

2, showing a static class category of singleton

/**
 * 外部类
 */
public class StaticInnerSingleton {
    /**
     * 私有的静态内部类
     */
    private static class SingletonClassInstance{
        private static final StaticInnerSingleton instance = new StaticInnerSingleton();
    }
    /**
     * 获取单例
     */
    public static  StaticInnerSingleton getInstance() {
        return SingletonClassInstance.instance;
    }
}




只要自己变优秀了,其他的事情才会跟着好起来(上将14)

Guess you like

Origin www.cnblogs.com/qdhxhz/p/11368870.html