Java 抽象类应该至少有一个抽象方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2283399/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:39:27  来源:igfitidea点击:

Should an abstract class have at least one abstract method?

javaabstract-classabstractabstract-methods

提问by java_geek

Is it necessary for an abstract class to have at least one abstract method?

抽象类是否必须至少有一个抽象方法?

采纳答案by matt b

The subject of this post and the body ask two different questions:

这篇文章的主题和正文提出了两个不同的问题:

  1. Should it have at least one abstract member?
  2. Is it necessary to have at least one abstract member?
  1. 它应该至少有一个抽象成员吗?
  2. 是否必须至少有一个抽象成员?

The answer to #2 is definitively no.

#2 的答案肯定是否定的。

The answer to #1 is subjective and a matter of style. Personally I would say yes. If your intent is to prevent a class (with no abstract methods) from being instantiated, the best way to handle this is with a privateprotectedconstructor, not by marking it abstract.

#1 的答案是主观的,是风格问题。我个人会说是的。如果您的意图是防止类(没有抽象方法)被实例化,那么最好的处理方法是使用构造函数,而不是通过标记它。privateprotectedabstract

回答by thecoop

No - you can declare a class abstract without having any abstract methods. It may not make any sense conceptually for an instance of that class to exist, or you may want to ensure that only subclasses of that class can be instantiated (for whatever reason)

不 - 您可以在没有任何抽象方法的情况下声明类抽象。该类的实例在概念上可能没有任何意义,或者您可能希望确保只能实例化该类的子类(无论出于何种原因)

回答by BalusC

No, it is not necessary. You see this often back in "template method"design pattern, like HttpServlet, wherein each method already has default behaviour definied and you're free to override just one (or more) of them instead of allof them.

不,没有必要。您经常在“模板方法”设计模式中看到这一点,例如HttpServlet,其中每个方法已经定义了默认行为,您可以自由地仅覆盖其中一个(或多个)而不是全部

回答by Tom Hawtin - tackline

In JDK 1.0 it was indeed necessary to have at least one abstract method in an abstract class. This restriction was removed in JDK 1.1 (1997? (I'm old)) and such classes added to the Java library, such as java.awt.event.KeyAdapter.

在 JDK 1.0 中,确实需要在抽象类中至少有一个抽象方法。这个限制在 JDK 1.1 (1997? (I'm old)) 中被删除了,这样的类被添加到 Java 库中,例如java.awt.event.KeyAdapter.

In C++ you need at least one pure virtual function to make a subclass necessary, and at least one virtual function to add RTTI to the class. Typically it makes sense to use the destructor.

在 C++ 中,您至少需要一个纯虚函数来创建必要的子类,并且至少需要一个虚函数来将 RTTI 添加到类中。通常使用析构函数是有意义的。

Note when overriding non-abstract methods, using @Overrideis a good idea. It not only tells the reader important information about what the code is attempting to do, but also spots common errors where typos or incorrect parameter types prevents the override.

请注意,在覆盖非抽象方法时,使用@Override是个好主意。它不仅告诉读者关于代码试图做什么的重要信息,而且还指出常见错误,其中拼写错误或不正确的参数类型会阻止覆盖。

回答by fastcodejava

If a class has an abstractmodifier on its declaration it becomes abstractclass.

如果一个类abstract在其声明上有一个修饰符,它就成为abstract类。