Day09私有成员内部类

159 阅读1分钟

创建类

public class outer {
	int a = 10;
	static int b = 20;
	
//	定义在成员属性位置的私有内部类
	private class inner{
		int c = 30;
		public void show() {
//			可使用外部类所有资源
			System.out.println(a);
			System.out.println(b);
			System.out.println(c);
		}
	}
	
	
	
	public void name() {
		System.out.println(a);//10
		System.out.println(b);//20
		inner inner = new inner();
		System.out.println(inner.c);//30
		inner.show();//10,20,30
	}
	

}

测试类

public class test {
	public static void main(String[] args) {
		/* new outer().new inner(); */ 
		outer outer = new outer();// 私有不可直接访问 需外部类创建对象通过外部类对象调用方法间接使用内部类
		outer.name();
	}

}