抽象类有什么用?为什么我们不能创建抽象类的对象? [英] what is the use of abstract class ? and why can't we create object of abstract class?

查看:94
本文介绍了抽象类有什么用?为什么我们不能创建抽象类的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抽象类有什么用?为什么我们不能创建抽象类的对象??

what is the use of abstract class ? and why can't we create object of abstract class??

推荐答案

你怎么能问为什么你不能创建一个抽象类的对象,如果整个目的是关键字 abstract 是为了防止创建此类对象。



请参阅: https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html [<一个href =https://docs.oracle.com/javase/tutorial/java/IandI/abstract.htmltarget =_ blanktitle =New Window> ^ ]。



抽象类的作用是作为其他类的基类。您可以将抽象类视为不完整的类,可以在其派生类中完成。一个微妙的时刻是关键字 abstract 的冗余,因为它可能是多余的,因为如果某些方法也是抽象的,那么实例的创建就没有多大意义,因此该类必须是抽象的;但没有假设;语法要求类也是抽象的。所以,我的不完整,在某些情况下,也可能意味着不够具体;最终,抽象类只有在最终有人用具体的方法覆盖所有抽象方法时才有意义。



这并不意味着抽象类不能没有任何抽象或者甚至没有虚拟方法 - 它可以,甚至可以有意义;你创建一个不完整的类,不应该实例化并完成某些派生类中的功能,希望不止一个,否则这种使用抽象的技术就没有多大意义。您只需使用抽象类作为一个方便的设备,以便以封装的方式重复使用公共代码。



但真正有趣的应用抽象类的使用是虚拟方法,抽象与否。然后,抽象类用作对象的多态集的基本编译时类。它充当多态集中所有元素的公共接口。这些对象的运行时类型应该从该抽象类派生。纯OOP方法是以类型不可知方式使用集合,因此对集合中所有元素的所有操作都是通过仅基类的属性完成的。这就是虚拟方法覆盖机制(动态调度)非常重要的原因。


请注意,这不是实现多态性的。另一种方法是使用Java 接口。您可以在上面引用的文章中阅读一些关于可以使用的内容的考虑因素。一个有趣的结果是:由于接口允许多重继承,同一个对象可以参与通过不同接口控制的多个多态集。但这是一个不同的故事...



-SA
How can you ask why you cannot create an object of abstract class, if the whole purpose of the keyword "abstract" is to prevent creation of such objects.

Please see: https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html[^].

The role of an abstract class is to serve as a base class for other classes. You can consider the abstract class as incomplete class which can be completed in its derived classes. One delicate moment is redundancy of the keyword "abstract" at a "class" is that it can be redundant, because, if some methods are also abstract, the creation of an instance would not make much sense, so the class is required to be abstract; but it is not assumed; the syntax requires the class to be abstract, too. So, my "incomplete", is some cases, may also mean "not concrete enough"; ultimately, the abstract class can only make sense if ultimately someone overrides all the abstract methods with concrete ones.

It does not mean that the abstract class cannot be without any abstract or without even virtual methods — it can be, and it can even make sense; you create an incomplete class which should not be instantiated and complete the functionality in some derived class, hopefully, more than one, otherwise this technique of using abstraction would not make much sense. You simply use the abstract class as a little convenient device for reusing of the common code in an encapsulated way.

But really interesting application of the use of abstract classes is with virtual methods, abstract or not. Then the abstract class is uses as a base compile-time class of a polymorphic set of objects. It serves as a common interface to all the elements in the polymorphic set. The runtime types of those objects should be derived from that abstract class. The pure OOP approach is to use the set in a type-agnostic way, so all the manipulations with all elements of the set are done through the properties of only the base class. That's why the virtual method override mechanism (dynamic dispatch) is critically important.

Note that this is not the only way of implementing polymorphism. The alternative way is using Java interfaces. You can read some consideration on what can be uses when in the article referenced above. One interesting consequence is: due to multiple inheritance allowed for interfaces, the same object can participate in more than one polymorphic sets controlled through different interfaces. But this is a different story…

—SA


抽象类是不完整的 类 - 它没有足够的信息可以实际使用它。



忽略计算机一会儿,想想一辆车 - 它可以有属性像Color和NumberOfWheels因为所有车都有颜色,车轮可以计算。但这并不意味着你可以将它带到商店,因为没有人会制造汽车。他们制作了福特嘉年华,梅赛德斯A级和劳斯莱斯幻影 - 并且只要你有钱可以驾驶它们。它们是具体类,它们源自您的许可证允许您驾驶的相同抽象概念汽车。



计算机类是相同的: Car类定义了基础知识,并允许派生类填写详细信息以创建可在代码中使用的内容。但抽象类本身没用,因为它无法定义引擎类型,驱动程序座位(左侧,右侧或中间?)或派生类将填充的任何其他详细信息对于特定的模型。

你的代码可以创建一个许可证,允许它驱动一个Car类,它将适用于任何派生类。
And abstract class is an "incomplete" class - it doesn't have enough information to actually be used as it is.

Ignore computers for a moment, and think about a car - it can have properties like Colour, and NumberOfWheels becasue all cars have a colour, and the wheels can be counted. But that doesn't mean you can drive it to the shops, because nobody makes "a Car". They make a "Ford Fiesta", a "Mercedes A Class", and a "Rolls Royce Phantom" - and provided you have the money you can drive each of them. They are the "concrete classes" which are derived from the same abstract concept "a car" which your licence permits you to drive.

Computer classes are the same: the Car class defines the basics, and lets the derived classes fill in the details to create something you can use in your code. But the abstract class is useless on it's own, because it can't define the engine type, the drivers seat positions (on the left, right, or in the middle?) or any of the other details which the derived class will fill in for the particular model.
And your code can create a "licence" which allows it to "drive" a Car class, that will work with any derived class.


这篇关于抽象类有什么用?为什么我们不能创建抽象类的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆