java 子类型在使用上与子类有何不同?

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

In what ways are subtypes different from subclasses in usage?

javasubclasssubtype

提问by Oh Chin Boon

A subtype is established when a class is linked by means of extending or implementing. Subtypes are also used for generics.

当一个类通过扩展或实现的方式被链接时,就建立了一个子类型。子类型也用于泛型。

How can I differentiate subtyping from subclasses?

如何区分子类型和子类?

采纳答案by Mike Samuel

In Java, subclassing is a kind of subtyping.

在 Java 中,子类化是一种子类型化。

There are a number of ways Java allows subtyping:

Java 允许子类型化的方式有很多:

  1. When class A extends B, Ais a subtype of Bbecause B b = new A(...);is ok.
  2. When interface A extends B, Ais a subtype of Bbecause B b = new A() { ... }is ok.
  3. When class A extends B, A[]is a subtype of B[]because B[] b = new A[0]is ok.
  4. When class A implements B, Ais a subtype of Bbecause B b = new A(...)is ok.
  1. class A extends B,AB因为的子类型B b = new A(...);是可以的。
  2. interface A extends B,AB因为的子类型B b = new A() { ... }是可以的。
  3. class A extends B,A[]B[]因为的子类型B[] b = new A[0]是可以的。
  4. class A implements B,AB因为的子类型B b = new A(...)是可以的。

It sounds like you want a way to distinguish one from the others. The below should do that.

听起来您想要一种方法来区分一个与其他。下面应该这样做。

static boolean isSubclass(Class<?> a, Class<?> b) {
  return !b.isArray() && !b.isInterface() && b.isAssignableFrom(a);
}

It won't handle subtyping of generic classes due to type erasure though. Classinstances don't carry type parameters at runtime so there is no way to distinguish the runtime type of a new ArrayList<String>()from a new ArrayList<Integer>().

由于类型擦除,它不会处理泛型类的子类型。 Class实例在运行时不携带类型参数,因此无法区分 anew ArrayList<String>()和 a的运行时类型new ArrayList<Integer>()

回答by Enrique Molinari

Subclass is not the same as subtype. You might create subclasses that are not subtypes. To understand what a subtype is, lets start giving an explanation of what a type is.

子类与子类型不同。您可以创建不是子类型的子类。要了解子类型是什么,让我们开始解释什么是类型。

When we say that the number 5 is of type integer, we are stating that 5 belongs to a set of possible values (as an example, see the possible values for the Java primitive types). We are also stating that there is a valid set of methods I can perform on the value like addition and subtraction. And finally we are stating that there are a set of properties that are always satisfied, for example, if I add the values 3 and 5, I will get 8 as a result.

当我们说数字 5 是整数类型时,我们是在说明 5 属于一组可能的值(例如,请参阅 Java 基本类型的可能值)。我们还声明有一组有效的方法可以对值执行,例如加法和减法。最后我们要说明有一组总是满足的属性,例如,如果我将值 3 和 5 相加,结果将是 8。

To give another example, think about the abstract data types, Set of integersand List of integers, the values they can hold are restricted to integers. They both support a set of methods, like add(newValue)and size(). And they both have different properties (class invariant), Setsdoes not allow duplicates while Listdoes allow duplicates (of course there are other properties that they both satisfy).

再举一个例子,想想抽象数据类型,整数集整数的名单,他们能够保存的值被限制为整数。它们都支持一组方法,例如add(newValue)size()。并且它们都有不同的属性(类不变),Sets不允许重复,而List允许重复(当然还有它们都满足的其他属性)。

Subtype is also a type, which has a relation to another type, called parent type (or supertype). The subtype must satisfy the features (values, methods and properties) of the parent type. The relation means that in any context where the supertype is expected, it can be substitutable by a subtype, without affecting the behaviourof the execution. Let's go to see some code to exemplify what I'm saying. Suppose I write a List of integers(in some sort of pseudo language):

子类型也是一种类型,它与另一种类型有关系,称为父类型(或超类型)。子类型必须满足父类型的特性(值、方法和属性)。关系意味着在任何需要超类型的上下文中,它都可以被子类型替换,而不会影响执行的行为。让我们去看一些代码来举例说明我在说什么。假设我写了一个整数列表(用某种伪语言):

class List {
  data = new Array();

  Integer size() {
    return data.length;
  }

  add(Integer anInteger) {
    data[data.length] = anInteger;
  }
}

Then, I write the Set of integersas a subclass of the List of integers:

然后,我将整数集为整数列表的子类:

class Set, inheriting from: List {
  add(Integer anInteger) {
    if (data.notContains(anInteger)) {
      super.add(anInteger);
    }
  }
}

Our Set of integersclass is a subclass of List of Integers, but is not a subtype, due to it is not satisfying all the features of the Listclass. The values, and the signature of the methods are satisfied but the properties are not. The behaviour of the add(Integer)method has been clearly changed, not preserving the properties of the parent type. Think from the point of view of the client of your classes. They might receive a Set of integerswhere a List of integersis expected. The client might want to add a value and get that value added to the Listeven if that value already exist in the List. But her wont get that behaviour if the value exists. A big suprise for her!

我们的Set of integers类是List of Integers的子类,但不是子类型,因为它不满足List类的所有特性。方法的值和签名得到满足,但属性不满足。add(Integer)方法的行为已经明显改变,没有保留父类型的属性。从班级客户的角度思考。他们可能会收到一组整数,其中需要一个整数列表。客户端可能想要添加一个值并将该值添加到列表中,即使该值已存在于列表中. 但如果价值存在,她就不会得到那种行为。对她来说是个大惊喜!

This is a classic example of an improper use of inheritance. Use composition in this case.

这是一个不正确使用继承的典型例子。在这种情况下使用组合。

(a fragment from: use inheritance properly).

(片段来自:正确使用继承)。

回答by Foo Bah

In general, subclassing means to inherit the attributes of a parent. Subtyping merely means that operations on the supertype can be performed on the subtype. Note that subclassing is a special case of subtyping.

一般来说,子类化意味着继承父类的属性。子类型只是意味着可以在子类型上执行对超类型的操作。请注意,子类化是子类型化的一种特殊情况。

in Java, interfaces represent the structure for describing what behaviors a type can exhibit, which makes it the natural representation for subtyping. Subclassing is manifested in the class hierarchy.

在 Java 中,接口表示用于描述类型可以表现出哪些行为的结构,这使其成为子类型的自然表示。子类化体现在类层次结构中。

回答by James P.

For once, Wikipedia gives a very straight answer to the question:

这一次,维基百科对这个问题给出了一个非常直接的答案:

http://en.wikipedia.org/wiki/Subtype_polymorphism

http://en.wikipedia.org/wiki/Subtype_polymorphism

Subtyping should not be confused with the notion of (class or object) inheritance from object-oriented languages; subtyping is a relation between types (interfaces in object-oriented parlance) whereas inheritance is a relation between implementations stemming from a language feature that allows new objects to be created from existing ones. In a number of object-oriented languages, subtyping is called interface inheritance.

子类型不应与面向对象语言的(类或对象)继承概念混淆;子类型是类型(面向对象术语中的接口)之间的关系,而继承是源于允许从现有对象创建新对象的语言特性的实现之间的关系。在许多面向对象的语言中,子类型被称为接口继承。

In short, subtyping occurs when you derive an interface (method signatures/access points/ways of reacting to the outside world) from another whereas subclassing occurs when you derive an implementation (methods, attributes/internal state and inside logic) of a class from another class through inheritance.

简而言之,当您从另一个派生接口(方法签名/访问点/对外部世界做出反应的方式)时,就会发生子类型,而当您从另一个派生一个类的实现(方法、属性/内部​​状态和内部逻辑)时,就会发生子类化。另一个类通过继承。

This terminology is not often used in this way and type usually refers to the data type.

该术语不常以这种方式使用,类型通常是指数据类型

回答by Jér?me Verstrynge

In java, subtypingapplies to interfaces, but subclassesdoes not apply to interfaces.

在java中,subtyping适用于接口,但subclasses不适用于接口。

回答by Arafangion

I don't think Java distinguishes between them? You have only classes.

我不认为Java区分它们?你只有课程。