JAVA内部类

/**
 * 
 */
package com.M.controller.test;

/**
 * @author 作者 : 谭健 (online zuozuo)
 * @date 创建时间:2017年3月3日 下午3:43:02
 * @version 1.0
 * @since
 * @use 本类用于:解释内部类,当他向上转型为实现一个接口,他可以让接口实现完全隐藏,且该实现不可用
 * @return
 */
public class InsideClassTest {

	private static Integer integer = 2017;
	Integer money = 12000;
	public Integer date = 2;

	/**
	 * 假设本类实现某个接口,他是一个私有的内部类
	 */
	private class Com {
		private Integer integerCom = 2018;

		public Integer comI() {
			return integerCom;
		}
	}

	public static void main(String[] args) {
		errT();
	}

	static void errT() {
		System.err.println(integer);

	}

	/**
	 * 内部类:他能访问其外围对象的所有成员,而不需要任何特殊条件
	 */
	class InsideClass {
		// 访问外部成员
		void err() {

			// 变量
			System.err.println(integer);
			System.err.println(money);
			System.err.println(date);

			// 方法
			errT();
			System.err.println(self());
			System.err.println(selfI());

		}

		/**
		 * 该方法没有任何运行时开销
		 * 
		 * @return
		 */
		InsideClassTest self() {
			return InsideClassTest.this;
		}

		/**
		 * 创建一个内部类的对象,该对象必须由外部类的对象来创建
		 * 
		 * @return
		 */
		InsideClass selfI() {
			InsideClassTest t = new InsideClassTest();
			InsideClassTest.InsideClass i = t.new InsideClass();
			return i;
		}

	}

	/**
	 * 静态内部类:作为一个嵌套类存在,他的创建不需要外部类的对象
	 */
	static class StaticInsideClass {

	}

}

 

转载于:https://my.oschina.net/TJALS/blog/855329

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值