如何创建抽象类和接口的对象
如何创建抽象类和接口的对象?我知道我们不能直接实例化抽象类的对象。
How do I create an object of an abstract class and interface? I know we can't instantiate an object of an abstract class directly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
不,您不会在这里创建抽象类的实例。相反,您正在创建抽象类的匿名子类的实例。然后您在指向子类对象的抽象类引用上调用该方法。
No, you are not creating the instance of your abstract class here. Rather you are creating an instance of an anonymous subclass of your abstract class. And then you are invoking the method on your abstract class reference pointing to subclass object.
你所知道的都是正确的。您不能创建抽象类或接口的对象,因为它们是不完整的类(接口甚至不被视为类)。
您可以做的是实现抽象类的子类,当然,抽象类不能是抽象的。对于接口,您必须创建一个实现该接口的类并实现接口方法的主体。
这里是oracle网站上的原始教程,http://download.oracle.com /javase/tutorial/java/IandI/abstract.html 和 http://download.oracle.com/javase/tutorial/java/concepts/interface.html
What you know is correct. You cannot create an object of abstract class or interface since they are incomplete class (interface is not even considered as a class.)
What you can do is to implement a subclass of abstract class which, of course, must not be abstract. For interface, you must create a class which implement the interface and implement bodies of interface methods.
Here are orginal tutorial on oracle site, http://download.oracle.com/javase/tutorial/java/IandI/abstract.html and http://download.oracle.com/javase/tutorial/java/concepts/interface.html
您不能实例化抽象类或接口,但可以实例化它们的子类/实现器之一。
您无法实例化抽象类或接口,只能实例化它们的派生类之一。
在您的示例中,
您正在实例化任何实现令人惊奇的类。
You can not instantiate the abstract class or an interface, but you can instantiate one of their subclasses/implementers.
You can't instantiate an abstract class or an interface, you can only instantiate one of their derived classes.
In your example
You are instantiating any class that implements Suprising.
不,我们不能从接口或抽象类创建对象,因为
创建对象的主要目的是利用包装的方法和数据。
由于接口没有任何具体的实现,因此我们不能。
对于抽象类,我们可能有具体方法或抽象方法或两者都有。
API 开发人员无法限制未实现的方法的使用。
希望有帮助。
NO, we can't create object out of an interface or Abstract class because
Main intention of creating an object is to utilize the wrapped methods and data.
As interface don't have any concrete implementation hence we cannot.
For abstract class we may have concrete method or abstract method or both.
There is no way for the API developer to restrict the use of the method thats don't have implementation.
Hope help.
“实例化”的意思是“创建一个对象”。
所以你不能直接创建一个。
接口和抽象类的目的是描述一些实现接口或扩展抽象类的具体类的行为。
实现接口的类可以被其他只知道接口的代码使用,这有助于您分离职责,并清楚您想要从对象中得到什么。 (调用代码只会知道该对象可以执行接口中指定的任何操作;它不会知道它具有的任何其他方法。)
如果您正在使用需要
Fooable
的其他人的代码(其中这是某个接口的名称),实际上并没有要求您提供某个 Fooable 类的对象(因为实际上并不存在这样的类)。您只是被要求提供某个实现 Fooable 的类的实例,即声明它可以执行该接口中的所有操作。简而言之,就是“可以被Foo'd”的东西。"instantiate" means "create an object of".
So you can't create one directly.
The purpose of interfaces and abstract classes is to describe the behaviour of some concrete class that implements the interface or extends the abstract class.
A class that implements an interface can be used by other code that only knows about the interface, which helps you to separate responsibilities, and be clear about what you want from the object. (The calling code will only know that the object can do anything specified in the interface; it will not know about any other methods it has.)
If you are using someone else's code that expects a
Fooable
(where that is the name of some interface), you are not really being asked for an object of some Fooable class (because there isn't really such a class). You are only being asked for an instance of some class that implements Fooable, i.e. which declares that it can do all the things in that interface. In short, something that "can be Foo'd".您编写一个派生自抽象类或实现该接口的类,然后实例化它。
You write a class that derives from the abstract class or implements the interface, and then instantiate that.
要创建抽象类的对象,只需使用new,就像创建其他非抽象类的对象一样,只有一点点不同,如下:
同样,您可以创建接口类型的对象,就像如下:
To create object of an abstract class just use new just like creating objects of other non abstract classes with just one small difference, as follows:
In the same way You can create an object of interface type, just as follows:
有两种方法可以实现这一目标。
1)要么在新类中扩展/实现抽象类/接口,创建这个新类的对象,然后根据您的需要使用该对象。
2) 编译器允许您在代码中创建接口的匿名对象。
例如。 ( 新的 Runnable() { ... } );
希望这有帮助。
问候,
马亨德拉·利亚。
There are two ways you can achieve this.
1) Either you extend / implement the Abstract class / interface in a new class, create the object of this new class and then use this object as per your need.
2) The Compiler allows you to create anonymous objects of the interfaces in your code.
For eg. ( new Runnable() { ... } );
Hope this helps.
Regards,
Mahendra Liya.
您可以提供一个实现作为匿名类:
同样,匿名类可以扩展父类而不是实现接口(但它不能两者兼而有之)。
You can provide an implementation as an anonymous class:
Likewise, an anonymous class can extend a parent class instead of implementing an interface (but it can't do both).
您不能实例化抽象类或接口 - 您可以实例化它们的子类/实现器之一。
这种情况的典型例子是 Java 集合的使用。
您使用接口类型
List
作为类型,但实例本身是ArrayList
。You can not instantiate an abstract class or an interface - you can instantiate one of their subclasses/implementers.
Examples of such a thing are typical in the use of Java Collections.
You are using the interface type
List<T>
as the type, but the instance itself is anArrayList<T>
.