内部类的应用

“多继承”

public class helloTest {
	public static void main(String[] args) {
		Outer out = new Outer();
		out.myInner1 = out.new Inner1();
		out.myInner2 = out.new Inner2();
		int value = out.getValue();
		System.out.println(value);
		return;
	}
}
class Class1 {
	protected int a = 1;
}
class Class2 {
	protected int b = 2;
}
class Outer{
	private int c = 3;
	Inner1 myInner1;
	Inner2 myInner2;
	class Inner1 extends Class1{
		private int d = 4;
		public int getValue() {
			return this.d + this.a;
		}
	}
	class Inner2 extends Class2{
		private int e = 5;
	}
	public int getValue() {
		return this.c + this.myInner1.getValue() + this.myInner2.b + this.myInner2.e;
	}

}

待实现的多个接口有相同的方法名时

* 要实现的2个接口 有相同的方法名 不能同时直接实现 */
interface Interface1{
	void onClick();
}
interface Interface2{
	void onClick();
}
/* 只能实现一个 */
class Class1 implements Interface1 {

	/* 第一个接口的方法实现 */
	@Override
	public void onClick() {
		// TODO Auto-generated method stub
		System.out.println("this is click from interface1");
	}
	
	/* 第二个接口的方法实现 --换个名字 */
	private void onClick2() {
		System.out.println("this is click form interface2");
	}
	/* 第二个通过内部类来实现 */
	class Inner implements Interface2{

		@Override
		public void onClick() {
			// TODO Auto-generated method stub
			/* 回调 !!!!! */
			onClick2();
			return;
		}
		
	}
	public Inner getInnerInstace() {
		return new Inner();
	}
}
public class helloTest {
	public static void main(String[] args) {
		Class1 c = new Class1();
		c.onClick();
		c.getInnerInstace().onClick();
		return;
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值