匿名内课 [英] Anonymous Inner class
本文介绍了匿名内课的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class One {
Two two() {
return new Two() {
Two(){}
Two(String s) {
System.out.println("s= "+s);
}
};
}
}
class Ajay {
public static void main(String ...strings ){
One one=new One();
System.out.println(one.two());
}
}
上面的示例代码无法编译。它说两个无法解决。
这段代码有什么问题??
The above sample code cannot be compiled.It says "Two cannot be resolved". What is the problem in this code??
推荐答案
new Two() {
Two(){}
Two(String s) {
System.out.println("s= "+s);
}
};
匿名内部类称为匿名,因为它没有自己的名称,必须引用通过它扩展/实现的基类或接口的名称。
An anonymous inner class is called anonymous because it doesn't have its own name and has to be referred to by the name of the base-class or interface it extends/implements.
在你的例子中,你创建一个的匿名子类
所以两个
必须在某个地方声明为类或接口。如果已经声明了第二类,则要么在类路径中没有它,要么忘记导入它。
In your example you create an anonymous subclass of Two
so Two
has to be declared somewhere either as a class or interface. If the class Two is already declared you either don't have it on your classpath or forgot to import it.
这篇关于匿名内课的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文