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("匿名内部类");
	    	}
	  	}
	  );
	}
}




你可能感兴趣的:(java--内部类匿名内部类.(第35集))