Java external class subclass inherits a member of the outer class inner class, brief description of the members of the inner class

package com.it.study;

public class TestMemberInnerClass {
	public static void main(String[] args) {
		Outer.UnstaticInnerClass varName = new Outer().new UnstaticInnerClass();
		varName.show();
		Outer out = new Outer();
		Outer.UnstaticInnerClass unsi = out.new UnstaticInnerClass();
		unsi.show();
		
		Outer.StaticInnerClass varName2 = new Outer.StaticInnerClass();
		System.out.println(varName2.age);
		System.out.println(varName2.staticPropIner);
		System.out.println(Outer.StaticInnerClass.staticPropIner);
		varName2.show();
//		Outer.StaticInnerClass si = new Outer().new Outer.StaticInnerClass();//当被封闭实例限定时,无法使用其复合名称分配成员类型Outer.StaticInnerClass。成员类型名称相对于限定实例类型进行解析
		
		
		OuterChild otc = new OuterChild();
		OuterChild.UnstaticInnerClass unsi2 = otc.new UnstaticInnerClass();
		OuterChild.StaticInnerClass varName3 = new OuterChild.StaticInnerClass();
	}
}
class OuterChild extends Outer{
}

class Outer{
	private int age = 10;
	private int outerProp = 10;
	public static int staticProp = 10;
	public void OuterSelfMethod() {
		System.out.println(this.new UnstaticInnerClass().age);
	}
	public static void OuterStaticSelfMethod() {
		System.out.println(Outer.StaticInnerClass.staticPropIner);
	}
	class UnstaticInnerClass{
		int age = 20;
		public void show() {
			int age = 30;
			System.out.println("内部类方法里的局部变量age:" + age);// 30
            System.out.println("内部类的成员变量age:" + this.age);// 20
            System.out.println("外部类的成员变量age:" + Outer.this.age);// 10
            System.out.println("外部类的成员变量outerProp:" + outerProp);
            new Outer().OuterSelfMethod();
		}
	}
	static class StaticInnerClass{
		int age = 10;
		static int staticPropIner = 10;
		public void show() {
			int age = 30;
			System.out.println("内部类方法里的局部变量age:" + age);// 30
            System.out.println("内部类的成员变量age:" + this.age);// 20
//            System.out.println("外部类的成员变量age:" + Outer.this.age);// 10
//            System.out.println("外部类的成员变量outerProp:" + outerProp);
            System.out.println("外部类的成员变量staticProp:" + Outer.staticProp);// 10
            System.out.println("外部类的成员变量staticProp:" + staticProp);
//            OuterSelfMethod();
            Outer.OuterStaticSelfMethod();
		}
	}
}

Can be modified with private, default, protected, public (and the general members of the same), class files: external internal class $ class .class
1, non-static inner classes (outside of class to use non-static inner classes and usually use the other class )
a) non-static inner classes must be deposited in an external class object. Therefore, rather than static internal class object must have a presence of external object. Non-static inner class object belongs to a separate external object (package, similar to one's own heart)
b) non-static inner classes can access the class members directly, but outside of class members can not access non-static inner class members.
c) a non-static inner classes can not have static methods, static properties and static initialization block. (Because it is non-static class member)
D) class external static method, a static code block, the non-static state can not access internal class, including the definition of variables and can not use it to create an instance.
Access e) member variable:
I local variables inside a class method: variable name
ii internal class attributes:.. the this variable name.
.. iii External class attributes: name .this class external variable names (attribute only if the class has external, may be used directly)
access f) within the class:
. I defined in an external class inner class: Inner new new ();
II . outside outer class using nonstatic inner classes: outer Outer.Inner varnme = new new () newInner ();.
2, the internal static class
a) defined method:
static class ClassName {// class body}
B) using the points:
I. When there is an internal static class object, do not necessarily exist corresponding to the external object. Thus, the internal static class instance method is not directly accessible outside the class instance method. (Static inner class is a static member of the class)
ii. Static inner class as a static member outside the class. Therefore, the method can be outside of class: access static member static inner class "static inner class name." Way, through new static inner class instance access static inner classes ().

Published 37 original articles · won praise 29 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_42755868/article/details/104876605