JAVA 学习第九天

//内部类
public class Outer {
	private static int x = 3;
	public static class Inner//内部类
	{
		static void function()
		{
			System.out.println(x);
//			System.out.println("inner class "+x);//6
//			System.out.println("inner class "+this.x);//4
//			System.out.println("inner class "+Outer.this.x);//3
		}
	}
	
	public static class Inner2
	{
		void show()
		{
			System.out.println("show");
		}
	}
	public void method()
	{
		//new Outer.Inner2().show();
		new Inner2().show();
	}
	
//	public void method()
//	{
//		Inner in = new Inner();
//		in.function();
//	}
}
//import javax.swing.JOptionPane;
/*
 * 内部类的访问规则:
 * 1,内部类可以直接访问外部类中的成员,包括私有。
 *    之所以可以直接访问外部类中的成员,是因为内部类中持有了一个外部类的引用,格式  外部类名.this
 * 2,外部类要访问内部类,必须建立内部类对象。
 * 
 * 访问类格式:
 * 1,当内部类定义在外部类的成员位置是,而且非私有,可以在外部其它类中。
 * 可以直接建立内部类对象。
 * 格式
 *    外部类名.内部类名   变量名 = 外部类对象.内部类对象 ;
 *    Outer.Inner in = new Outer().new Inner();
 * 2,当内部类在成员位置上,就可以被成员修饰符所修饰。
 *    比如,private : 将内部类在外部类中封装。
 *        static :内部类就具备static的特性。
 *        当内部类被static修饰以后,只能直接访问外部类中的static成员,出现了访问局限。
 *        
 *        在外部其它类中如何直接访问static内部类的非静态成员呢
 *        new Outer.Inner().function();
 *        
 *        
 *        在外部其它类中如何直接访问static内部类的静态成员呢
 *        Outer.Inner.function();
 *    注意:当内部类中定义了静态成员,该内部类必须是static的。
 *        当外部类中的静态方法访问内部类时,内部类也必须是static的
 *        
 * 当描述事物时,事物的内部还有事物,该事物用内部类来描述,
 * 因为内部事物在使用外部事物的内容。
 * class Body
 * {
 * 		private class Heart
 * 		{
 * 			
 * 		}
 * 		
 * 		public void show()
 * 		{
 * 			new Heart();
 * 		}
 * }
 */
public class Welcome {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//Outer a = new Outer();
		//a.method();
		//直接访问内部类中的成员
//		Outer.Inner a = new Outer().new Inner();
//		a.function();
//		new Outer.Inner().function();
		new Outer().method();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值