Java 抽象类的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18438474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Instance of an abstract class
提问by Kifsif
Couldy you clarify why this works:
你能澄清一下为什么会这样吗:
public abstract class AbstractClassCreationTest {
public void hello(){
System.out.println("I'm the abstract class' instance!");
}
public static void main(String[] args) {
AbstractClassCreationTest acct = new AbstractClassCreationTest(){};
acct.hello();
}
}
I suppose it contradicts to the specification where we can find:
我想它与我们可以找到的规范相矛盾:
It is a compile-time error if an attempt is made to create an instance of an abstract class using a class instance creation expression (§15.9).
如果尝试使用类实例创建表达式(第 15.9 节)创建抽象类的实例,则会出现编译时错误。
回答by Joe Taras
An abstract class cannot instantiated.
抽象类不能实例化。
You must create an extension class extends an abstract class and so istantiated this new class.
您必须创建一个扩展类来扩展一个抽象类,从而对这个新类进行实例化。
public abstract class AbstractClassCreationTest {
public void hello(){
System.out.println("I'm the abstract class' instance!");
}
}
public class MyExtClass extends AbstractClassCreationTest() {
}
public static void main(String[] args) {
MyExtClass acct = new MyExtClass(){};
acct.hello();
}
I post this. Can be useful for you. Have a nice day
我张贴这个。可以对你有用。祝你今天过得愉快
回答by S.D.
You may have not noticed the difference:
你可能没有注意到区别:
new AbstractClassCreationTest(){};
versus
相对
new AbstractClassCreationTest();
That extra {}
is the body of a new, nameless class that extends the abstract class. You have created an instance of an anonymous classand not of an abstract class.
这个额外{}
的部分是一个新的、无名的类的主体,它扩展了抽象类。您创建了匿名类的实例,而不是抽象类的实例。
Now go ahead an declare an abstract method in the abstract class, watch how compiler forces you to implement it inside {}
of anonymous class.
现在继续在抽象类中声明一个抽象方法,观察编译器如何强制您在{}
匿名类中实现它。
回答by rocketboy
Notice the difference between:
注意以下区别:
AbstractClassCreationTest acct = new AbstractClassCreationTest(){};//case 1
NonAbstractClassCreationTest acct = new NonAbstractClassCreationTest();//case 2
case1is an anonymous classdefinition. You are notinstantiating an abstract class; instead you are instantiating a subTypeof said abstract class.
case1是一个匿名类定义。你不是在实例化一个抽象类;而不是你实例化一个子类型表示抽象类。
回答by Krushna
Here you are not creating the object of the AbstractClassCreationTest
class , actually you are creating an object of anonymous inner class who extends the AbstractClassCreationTest
class.This is because you have written new AbstractClassCreationTest(){} not new AbstractClassCreationTest()
You can know more about anonymous inner class from here
在这里你不是在创建AbstractClassCreationTest
类的对象,实际上你是在创建一个扩展类的匿名内部类的对象AbstractClassCreationTest
。这是因为你写了new AbstractClassCreationTest(){} not new AbstractClassCreationTest()
你可以从这里了解更多关于匿名内部类的信息