为什么抽象方法是抽象的?

huangapple go评论76阅读模式
英文:

Why are abstract methods abstract?

问题

"对于某物而言,抽象意味着隐藏数据。我不明白抽象方法如何对用户隐藏任何数据。可以提出的论点是抽象方法不需要实现,但在类层次结构的大体系中,这并不对用户隐藏任何信息。抽象方法似乎只是强制实现,所以我不明白为什么它们是抽象的。"

英文:

For something to be abstract means to hide data. I don't understand how abstract methods hide any data from the user. An argument can be made that abstract methods require no implementation but in the grand scheme of a class hierarchy that does not hide anything from the user. Abstract methods seem to just force implementation so I do not understand why they are abstract.

答案1

得分: 6

尽管抽象类和方法可以用于数据隐藏,“抽象”并不意味着“隐藏数据”。Java语言规范定义了抽象类和方法如下:

抽象类是一个不完整的类,或者可以认为是不完整的。

抽象方法声明引入了该方法作为一个成员,[...],但不提供实现(§8.4.7)。一个不是抽象的方法可以被称为具体方法。

抽象类可以用于数据隐藏。例如,java.awt.Toolkit是一个抽象类,但其具体实现是私有的。要使用Toolkit,您可以使用静态工厂方法getDefaultToolkit获取一个实例,该方法返回一个具体类的实例,但您不知道是哪个类。通过提供一个公共的抽象Toolkit类,您可以使用类提供的服务,但“数据”(具体实现)对您是隐藏的。

英文:

Although abstract classes and methods can be used for data hiding, "abstract" does not mean "hide data". The Java language specification defines abstract classes and methods like this:

> An abstract class is a class that is incomplete, or to be considered incomplete.

> An abstract method declaration introduces the method as a member, [...], but does not provide an implementation (§8.4.7). A method that is not abstract may be referred to as a concrete method.

Abstract classes can be used for data hiding. For example, java.awt.Toolkit is an abstract class, but its concrete implementations are private. To use Toolkit, you get an instance using the static factory method getDefaultToolkit, which returns an instance of a concrete class but you don't know which one. By providing a public abstract Toolkit class, you can use the services the class provides, but the "data" (the concrete implementation) is hidden from you.

答案2

得分: 2

"对于某物来说抽象意味着隐藏数据。

我认为你的困惑源于这个(错误的)前提。

抽象更好地理解为没有实现。它是具体的相反(即有实现)。

请参阅维基百科上的抽象类型文章:

在编程语言中,抽象类型是在命名类型系统中不能直接实例化的类型;而不是抽象的类型 - 可以实例化的类型 - 被称为具体类型。

英文:

>For something to be abstract means to hide data.

I think your confusion comes from this (wrong) premise.

Being abstract is better understood as having no implementation. It's the opposite of concrete (i.e. with implementation).

See the abstract type article from Wikipedia:

>In programming languages, an abstract type is a type in a nominative type system that cannot be instantiated directly; a type that is not abstract – which can be instantiated – is called a concrete type.

答案3

得分: 1

使用“抽象”的实际定义时,Java抽象方法的命名更有意义:

存在于思想中或作为一个概念,但没有具体的物理存在

这是有道理的,因为Java抽象方法是没有实现的方法定义。

英文:

I think when you use the actual definition of abstract, the nomenclature of Java abstract methods makes more sense:

> existing in thought or as an idea but not having a physical or concrete existence

This makes sense since Java abstract methods are method definitions without implementations.

答案4

得分: 0

术语“抽象”意味着某物并非有形,而更像是一个概念。而实现则是可以使用的实际事物。单词“具体”用来描述后者。

“抽象类”是具有零个或多个已实现方法和零个或多个未实现方法的类,用于构建具有更多或专门功能的其他类。因此,子类化抽象类的任何类可能具有以下内容:

  1. 从抽象类继承的一组核心实现方法,这些方法不一定会在不同的实现之间变化。
  2. 由抽象类指定的方法签名(未实现的方法),必须在子类中实现,并且其实现可能根据子类要求在不同的实现之间有所不同。
  3. 子类的作者认为必要但在抽象类中未指定的任何其他附加方法。

上述的1和2构成了抽象类。

在1中,如果子类的作者认为有必要,可以重写这些方法(除非这些方法被声明为“final”)。

最后,抽象类可以继承其他抽象类,采用了上述相同的哲学。

英文:

The term abstract implies that something isn't tangible but more of a concept. Whereas an implementation is an actual thing that can be used. The word concrete is used to describe the latter.

Abstract classes are classes that have a mixture of zero or more implemented and zero or more non implemented methods that are used to build other classes with more or specialized functionality. So any class that subclasses an Abstract class may have:

  1. A set of core implemented methods inherited from the Abstract class that would not necessarily vary from one implementation to another.
  2. A set of method signatures (non implemented methods) specified by the Abstract class that must be implemented in subclasses and whose implementations may differ from one implementation to another depending on the subclass requirements.
  3. Any other additional methods that the authors of the subclass feel are required that are not specified in the Abstract class.

1 and 2 above make up the abstract class.

In the case of (1) above, those methods may be overridden if the author of the subclass deems it is necessary (unless those methods are declared final).

Finally, Abstract classes may subclass other Abstract classes employing the same philosophy above.

huangapple
  • 本文由 发表于 2020年8月1日 06:51:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63200026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定