Java中的抽象类和方法,继承

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

Abstract classes and methods in Java, Inheritance

javainheritanceabstract-class

提问by user42155

I have class B, which inherits from class A. The superclass A is abstract, containing one abstract method. I don't want to implement the abstract method in class B, therefore I need to declare class B as abstract as well. Declaring class B abstract, two things are working for me (the programs compile and run correctly):

我有 B 类,它继承自 A 类。超类 A 是抽象的,包含一个抽象方法。我不想在 B 类中实现抽象方法,因此我也需要将 B 类声明为抽象方法。声明 B 类抽象,有两件事对我有用(程序编译和运行正确):

1.) I don't declare any abstract methods in class B, even thought the class is abstract. This works, I assume, because the class inherits the abstract method of class A, and this is enough for the class to be declared as abstract: we don't need any other abstract methods directly declared in the class.

1.) 我没有在 B 类中声明任何抽象方法,即使认为这个类是抽象的。我认为这是有效的,因为该类继承了类 A 的抽象方法,这足以将该类声明为抽象类:我们不需要在类中直接声明任何其他抽象方法。

2.) I do declare the same abstract method in class B as it is declared in class A. This is some kind of overriding (?), not in the same sense as overriding in java (using the same header, but providing different implementation), here I just use again the same header of the method.

2.) 我确实在类 B 中声明了与在类 A 中声明相同的抽象方法。这是某种覆盖(?),与 java 中的覆盖不同(使用相同的标头,但提供不同的实现) ),这里我只是再次使用该方法的相同标题。

Both things are working, and I am not sure whether they are both Ok, and whether some of them is preferred (more correct) that the other. Are the two ways the same (do they mean the same to Java)?

这两件事都在工作,我不确定它们是否都可以,以及其中一些是否比另一个更受欢迎(更正确)。这两种方式是否相同(它们对 Java 的意义是否相同)?

Here I give some example classes, so that what I mean is more clear for you:

在这里,我给出了一些示例类,以便您更清楚我的意思:

Case 1.):

情况1。):

public abstract class A {
    public abstract String giveSum();
}

public abstract class B extends A {

}

Case 2.):

情况 2.):

public abstract class A {
    public abstract String giveSum();
}

public abstract class B extends A {
    public abstract String giveSum();
}

Regards

问候

回答by Yoni Roit

They are functionally equal, but the first one is preferred because it's shorter and isn't weird.

它们在功能上是相同的,但第一个是首选,因为它更短而且并不奇怪。

回答by Zach Langley

Go with #1. Rewriting the method declaration in the child class is confusing. And you actually don't need any abstract methods in an abstract class, regardless of whether the parent is abstract or not.

和#1一起去。重写子类中的方法声明令人困惑。而且您实际上不需要抽象类中的任何抽象方法,无论父类是否是抽象的。

回答by OscarRyz

So, the question is: Which is preferred when sub classing an abstract class in java and don't want to provide implementation?

所以,问题是:在java中对抽象类进行子类化并且不想提供实现时,哪个是首选?

a) mark subclass as abstract too

b) mark subclass as abstract too AND re-write the method signature marked as abstract?

a) 也将子类标记为抽象

b) 也将子类标记为抽象并重写标记为抽象的方法签名?

I would go for the first one:

我会选择第一个:

a) Mark subclass as abstract too.

a) 也将子类标记为抽象。

The former has already the abstract method declaration, there is no point in repeating it.

前者已经有抽象方法声明了,这里不再赘述。

回答by Jared

In Java, the abstractclass annotation indicates that the class cannot be directly instantiated. A class could be declared abstractsimply because it should never be instantiated (perhaps it contains only static methods), or because its subclasses should be instantiated instead.

在Java中,abstract类注解表示不能直接实例化该类。可以声明一个类,abstract因为它永远不应该被实例化(也许它只包含静态方法),或者因为它的子类应该被实例化。

It is nota requirement that abstractclasses contain abstractmethods (the inverse istrue: a class containing one or more abstractmethods must be abstract.)

它是不是一个要求,即abstract类包含abstract的方法(逆真实的:一个包含类的一个或多个abstract方法必须是abstract)。

The question of whether you should duplicate the abstract method definition might be perceived as a style question - but I would be hard pressed to come up with an argument in favor of duplicating the definition (the only argument I can come up with is in the case where the class hierarchy might change the semantics or use of the method, and thus you'd like to provide an additional javadoc in class B.)

您是否应该复制抽象方法定义的问题可能会被视为一个风格问题 - 但我很难提出支持复制定义的论点(我能提出的唯一论点是在这种情况下类层次结构可能会改变语义或方法的使用,因此您希望在类 B 中提供额外的 javadoc。)

The primary argument against re-definition of the abstractmethod is that duplicate code is bad - it makes refactoring more cumbersome and such (all the classic "don't duplicate code" arguments apply.)

反对重新定义abstract方法的主要论点是重复代码是不好的——它使重构更加麻烦等(所有经典的“不要重复代码”参数都适用。)

回答by Arne Burmeister

You are right, the two cases are equivalent. Case 1) is more simple, case 2) is code duplication - avoid it. But there may be one reason to do so:

你是对的,这两种情况是等价的。情况 1) 更简单,情况 2) 是代码重复 - 避免它。但这样做可能有一个原因:

If the method in class A does not return Stringbut lets say C, class B may override it (since Java 5) with a more specific return type, lets say D (class extends C):

如果类 A 中的方法不返回String但可以说是 C,则类 B 可以使用更具体的返回类型覆盖它(自 Java 5 起),例如 D(类扩展 C):

public abstract class A {
  public abstract C giveSum();
}

public abstract class B extends A {
  public abstract D giveSum();
}

public class C {
  ...
}

public class D extends C {
  ...
}