使用没有任何抽象方法的抽象类 [英] Use of an abstract class without any abstract methods
问题描述
抽象类不需要包含任何抽象方法.
An abstract class need not include any abstract methods.
除了不能实例化抽象类之外,还有什么其他原因可以使类抽象?
Is there any other reason to make a class abstract other than the fact that abstract classes can't be instantiated?
推荐答案
抽象类的主要作用是提供一个适当的根类,可以从中派生出具体的(即非抽象的)子类.这是一个强大且通用的功能,可促进代码重用.抽象类封装了一系列数据类型共有的通用特性——这些特性在抽象类中太通用而没有意义,但可以在子类中覆盖
The principal role of an abstract class is to provide an appropriate root class from which concrete, (i.e. non-abstract) subclasses can be derived. This is a powerful and versatile feature which promotes code re-use. Abstract classes encapsulate general features that are common to a range of data types - features which are too general to be meaningful in the abstract class, but which can be overridden in a subclass
任何具有抽象方法的类都会自动抽象自身,并且必须使用关键字 abstract 定义自己——有趣的是,抽象类不需要包含任何抽象方法
Any class with an abstract method is automatically abstract itself and must define itself as such with the keyword abstract - interestingly, an abstract class need not contain any abstract methods
抽象类不能被实例化——换句话说,你不能创建抽象类的实例(对象)
An abstract class cannot be instantiated - in other words you cannot create instances (objects) of an abstract class
即使抽象类的对象无法实例化,也可以声明对抽象类的对象的引用,例如 Account a ;不会产生语法错误
References to objects of an abstract class can be declared even though objects of abstract classes cannot be instantiated, e.g Account a ; will not generate a syntax error
如果抽象类的子类覆盖,即提供其超类中每个抽象方法的实现,则子类称为具体类,并且可以创建子类的对象
If a subclass of an abstract class overrides, i.e. provides an implementation of every abstract method in its superclass, the subclass is called a concrete class and objects of the subclass can be created
如果抽象类的子类没有覆盖(实现)它继承的所有抽象方法,则该子类本身也是抽象的,必须如此声明
If a subclass of an abstract class does not override (implement) all of the abstract methods it inherits, that subclass itself is also abstract and must be declared as such
这篇关于使用没有任何抽象方法的抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!