问题描述
我对抽象类有疑问。
首先...我正在使用Google翻译,对不起:(希望您能理解。
First of all ... I'm using the google translator, sorry :( I hope you understand.
如果无法实例化抽象类,那么确切的代码是什么:
If abstract classes can not be instantiated, exactly, what is this code:
public class Ppal {
public void start(){
ABS clsAbs = new ABS() {
@Override
public void absMetod() {
}
};
clsAbs.metod();
}
}
ABS:
public abstract class ABS{
public void metod(){}
public abstract void absMetod();
}
ABS clsAbs = new ABS(){...
这不是实例吗?可以使用clsAbs和
不能使用抽象类,只能用来创建抽象模型...
ABS clsAbs = new ABS () {...
Is not this an instance? clsAbs can be used andAbstract classes can not be used, only to create a model of abstraction ...
它可以用作匿名类,但这( ABS clsAbs =新的ABS(){...
)不是匿名的。
It could be used as anonymous class but this (ABS clsAbs = new ABS () {...
) not anonymous.
非常感谢!
推荐答案
您正在实例化一个匿名类这扩展了 ABS
类。您可以实例化此匿名类,因为它定义了 ABS
(感谢Nebelmann)
You are instantiating an anonymous class that is extending the class ABS
. And you can instantiate this anonymous class because it defines all the abstract methods in ABS
(Thanks Nebelmann)
如果创建类似于 public class Foo的类,则该类扩展了实现
absMetod()
,您可以执行以下操作: ABS bar = new Foo()
and you can do: ABS bar = new Foo()
这篇关于如果不能实例化抽象类,那么确切的代码是什么:ABS clsAbs = new ABS(){的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!