Java_内部类

内部类 , 类里面还有类

out.inner oi=new out().new inner()//定义
class out{
	class inner{
		
	}
}

 内部类访问特点
 * a:内部类可以直接访问外部类的成员,包括私有。
 * b:外部类要访问内部类的成员,必须创建对象。

 

成员内部类被静态修饰后的访问方式是:(静态只需要一路‘.’ ,不用new)
 * 外部类名.内部类名 对象名 = new 外部类名.内部类名();

out.inner oi=out().new inner()   ---写成-->out.inner oi=new out().inner()
class out{
	static class inner{
		
	}
}

 

 

要求:使用已知的变量,在控制台输出30,20,10。
		
		class Outer {
			public int num = 10;
			class Inner {
				public int num = 20;
				public void show() {
					int num = 30;
					System.out.println(??);//num
					System.out.println(??);//this.num
					System.out.println(??);//Outer.this.num
				}
			}
		}
		class InnerClassTest {
			public static void main(String[] args) {
				Outer.Inner oi = new Outer().new Inner();
				oi.show();
			}	
		}

 

posted @ 2016-10-18 23:18  Lyxin_c  阅读(143)  评论(0编辑  收藏  举报