简体   繁体   English

使用没有任何抽象方法的抽象类

[英]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? 除了抽象类无法实例化这一事实之外,还有其他原因使类抽象化吗?

Abstract class means the definition of the class is not complete and hence cannot be instantiated. 抽象类意味着类的定义不完整 ,因此无法实例化。 Even though it does not have abstract method, it is an indicator that the class is available for inheritance . 尽管它没有抽象方法,但它表明该类可用于继承 Even though it has implementation for all the methods in it, the implementation may still not be complete and must be overridden by the extending class. 尽管它具有其中所有方法的实现,但实现可能仍然不完整,并且必须由扩展类覆盖。

You can make a class as abstract ,even if you don't want to implement all of the interface methods that the class implements . 即使您不想实现该类实现的所有接口方法,也可以将类设为抽象类。 According to java docs . 根据java 文档

It was noted that a class that implements an interface must implement all of the interface's methods. 有人指出,实现接口的类必须实现所有接口的方法。 It is possible, however, to define a class that does not implement all of the interface methods, provided that the class is declared to be abstract. 但是,如果声明该类是抽象的,则可以定义一个不实现所有接口方法的类。

Any way you can't instantiate a class that declared with abstract . 无论如何你都无法实例化一个用abstract声明的类。

Probably not. 可能不是。 It would prevent a "static helper" class from being instantiated, but I think using it that way would be poor style. 它会阻止“静态助手”类被实例化,但我认为使用它会是一种糟糕的风格。

As others say, if it's intended to be a base class -- with descendants declaring & implementing interfaces/ functionality, which is too specific for the superclass -- then abstract could be appropriate in the superclass. 正如其他人所说,如果它打算成为一个基类 - 后代声明和实现接口/功能,这对超类来说太具体了 - 那么abstract可能适合超类。

In that case it's a declaration that the super is not useful on it's own, and that subclasses will do something more -- which they will specifically declare. 在这种情况下,它是一个声明,超级对它自己没用,并且子类将做更多的事情 - 他们将特别声明。

The principal role of an abstract class is to provide an appropriate root class from which concrete, (ie 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 抽象类封装了一系列数据类型通用的一般特性 - 这些特性在抽象类中过于笼统而无意义,但可以在子类中重写

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 任何具有抽象方法的类本身都是自动抽象的,并且必须使用关键字abstract定义自己 - 有趣的是,抽象类不需要包含任何抽象方法

An abstract class cannot be instantiated - in other words you cannot create instances (objects) of an abstract class 抽象类无法实例化 - 换句话说,您无法创建抽象类的实例(对象)

References to objects of an abstract class can be declared even though objects of abstract classes cannot be instantiated, eg Account a ; 即使抽象类的对象无法实例化,也可以声明对抽象类对象的引用,例如Account a; will not generate a syntax error 不会生成语法错误

If a subclass of an abstract class overrides, ie 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 如果抽象类的子类没有覆盖(实现)它继承的所有抽象方法,那么该子类本身也是抽象的,必须声明为

There cannot be an abstract function in an non abstract class! 非抽象类中不能有抽象函数!

So if you need an abstract function in your class for whatever reason you must declare your class to be abstract. 因此,如果您因为某种原因需要在类中使用抽象函数,则必须将您的类声明为抽象类。

Why would I need an abstract function? 为什么我需要一个抽象函数?

What is the point of using abstract methods? 使用抽象方法有什么意义?

If an abstract class may not include any abstract method. 如果抽象类可能不包含任何抽象方法。 Than is there any other reason to make class abstract except of abstract class can't be instantiated 除了抽象类之外,没有任何其他理由使类抽象无法实例化

As for your question a scenario I can think of is as follows - 至于你的问题,我能想到的情况如下 -

Lets say all you need is static functionality of a class. 让我们说你需要的只是一个类的静态功能。 Static variables and function. 静态变量和函数。 So basically you don't want users to create any instance of your class. 所以基本上你不希望用户创建你的类的任何实例。 Now you can make your constructor private thereby ruling out the possibility of creating it's instance but you don't want to do that either. 现在你可以将你的构造函数设为私有,从而排除创建它的实例的可能性,但你也不想这样做。 Why write additional lines of code? 为什么要写额外的代码行? In this case you can make your class abstract. 在这种情况下,您可以使您的类抽象化。

I have extracted below points from Why should I declare a class as an abstract class? 我从下面提取了以下几点我为什么要将一个类声明为一个抽象类?

  1. Abstract classes improve the situation by preventing a developer from instantiating the base class, because a developer has marked it as having missing functionality. 抽象类通过阻止开发人员实例化基类来改善这种情况,因为开发人员已将其标记为缺少功能。

  2. It also provides compile-time safety so that you can ensure that any classes that extend your abstract class provide the bare minimum functionality to work, and you don't need to worry about putting stub methods that inheritors somehow have to magically know that they have to override a method in order to make it work. 它还提供编译时安全性,以便您可以确保扩展抽象类的任何类提供最低限度的工作功能,并且您不必担心将继承者以某种方式必须神奇地知道它们具有的存根方法覆盖方法以使其工作。

Abstract classes serve as templates for their subclasses. 抽象类充当其子类的模板。 For example, the abstract class Tree and subclass, Banyan_Tree, has all the characteristics of a tree as well as characteristics that are specific to the banyan tree. 例如,抽象类Tree和子类Banyan_Tree具有树的所有特征以及特定于banyan树的特征。

Understanding the differences between an abstract class and an interface is essential. 理解抽象类和接口之间的差异至关重要。 An interface only has method declarations or abstract methods and constant data members, while an abstract class may have abstract methods, member variables and concrete methods. 接口只有方法声明或抽象方法和常量数据成员,而抽象类可能有抽象方法,成员变量和具体方法。 Because Java only supports single inheritance, a class can implement several interfaces but can extend only one abstract class. 因为Java仅支持单继承,所以类可以实现多个接口,但只能扩展一个抽象类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM