java--内部类匿名内部类.(第35集)

以下内容来自marschen的java4android视频教程

        1.什么是内部类.

2.内部类的使用方法.
3.匿名内部类的使用方法:

class A{
	int i;
	class B{
		int j;
		//内部类是可以随意使用外部类的成员变量
		int funB(){
			//int result = i + j;
			int result = A.this.i + this.j;
			return result;
		}
	}
}


class Test{
	public static void main(String args[]){
		A a = new A();
		
		//A.B b = new A().new B();
		A.B b = a.new B();
		
		a.i = 3;
		b.j = 1;
		int result = b.funB();
		System.out.println(result);
	}
	
}


interface B{
	
	public void doSomething();

}

class C{
	public void fun(B b){
		System.out.println("C 类的fun函数 ");
		b.doSomething();
	}
}

//B的实现类
class AImpl implements B{
	public void doSomething(){
		System.out.println("doSomething");
	}
}



class TestBC {
	  public static void main(String args[]){
	 /* AImpl al = new AImpl();
	  B b = al;
	  C c = new C();
	  c.fun(b);*/
	  
	  C c = new C();
	  c.fun(new B(){
	    public void doSomething(){
	    		System.out.println("匿名内部类");
	    	}
	  	}
	  );
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值