`
nihao620
  • 浏览: 61475 次
  • 性别: Icon_minigender_2
社区版块
存档分类
最新评论

匿名内部类

F# 
阅读更多

<!----><!----> <!---->匿名的内部类是没有名字的内部类。可以继承一个类,但不能extends( 继承)多个 类,可以实现一个接口,但不可以实现多个接口。 

 

看下面这个例子:

 

 

public class Out{
	public Out(){
		//匿名内部类1:继承了S类
		new S(8){
			void f1(){
				System.out.println("extends S in inner class");
			}
		}.f1();

		//匿名内部类2:实现了接口If
		new If(){
			public void f(){
				System.out.println("implements f in inner class");
			}
		}.f();
		
	}

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

class S{
	int i;
	public S(){};
	public S(int i){
		this.i=i;
		System.out.println("i="+i);
	}
}

interface If{
	public void f();
}
 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics