Java中的抽象类和方法,继承 [英] Abstract classes and methods in Java, Inheritance

查看:286
本文介绍了Java中的抽象类和方法,继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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):

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

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.

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

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.

这两件事都有效,我不确定是否它们都是好的,并且它们中的一些是否比另一个更受欢迎(更正确)。这两种方式是否相同(它们对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)?

这里我给出一些示例类,所以我的意思更清楚:

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

案例1 。):

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

public abstract class B extends A {

}

案例2。):

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

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

问候

推荐答案

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

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

要求 abstract 类包含 abstract 方法(逆 true:包含一个或多个 abstract 方法的类必须是 abstract 。)

It is not a requirement that abstract classes contain abstract methods (the inverse is true: a class containing one or more abstract methods must be abstract.)

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

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.)

重新定义 abstract 方法的主要参数是重复代码很糟糕 - 它使重构变得更加繁琐(所有经典的不重复代码参数都适用。)

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

这篇关于Java中的抽象类和方法,继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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