"return a type that is compatible" 是什么意思?
What does it mean "return a type that is compatible"?
我是 Java 的新手,所以我正在阅读 Java Head First。我已经看到当你有一个带有抽象方法的抽象 class 时,你应该在一个具体的 class 中覆盖这些抽象方法,这意味着 "create a non-abstract method in your class with the same method signature (name and arguments) and a return type that is compatible with the declared return type of the abstract method."
我可以清楚地理解关于具有相同签名(名称和参数)的第一部分,但我想对 a return 类型有一个明确的解释与抽象方法声明的 return 类型兼容。
兼容类型到底是什么意思?有人可以举个例子吗?好像return类型应该是抽象方法中定义的return类型的class或者subclass?
覆盖方法返回的类型必须相同,或者必须是基方法返回类型的子类或子接口。
简而言之:它必须尊重基方法的契约。如果基本方法说:"I return a Fruit",那么覆盖方法可以说 "I return a Fruit",但它也可以说 "I return a Banana"。
没关系,因为香蕉是水果。任何调用该方法并获得香蕉的人都很高兴:期望得到一个水果,并且收到了一个水果。
虽然还车是不正确的,因为当你要水果时,得到车是不可接受的。
这个的技术术语是 covariant return type。请注意,即使基数 class/method 不是抽象的,这条规则也是正确的。
如果您的 return 类型是 A
,那么您还可以 return 来自 A
的任何 class B
的对象。所有这些都是兼容类型。
您的 return 类型也可以是接口。在这种情况下,您可以 return 任何实现此接口的对象。
这意味着您对抽象方法的实现必须 return 与抽象 class 中定义的对象相同或派生 class
例子:
你有一个带有抽象方法的抽象类
abstract class AbstractTest{
abstract Date getInitialTime();
}
那么你有一个测试class。
class Test extends AbstractTest{
...
Date getInitialTime(){
return new Date();
}
}
您重写了 getInitialTime
方法,该方法的 return 必须是 class 日期或 [=13] 的超class 的对象=]
如果你 return 其他东西,那么你就是在破坏你的 class return 方法和 os 父方法之间的契约 class returns...
考虑下面的例子。抽象函数model()
的return类型是int
。
abstract class Bike{
abstract short model();
}
- 当您在扩展抽象 class
Bike
的具体 class 中重新定义此方法时。具体 class 应该有一个具有相同方法签名和兼容 return 类型的方法 model()
。
One return type is compatible with another, if it doesn't lead to loss
of precision.
One return type compatible with short
is int
.
class Honda4 extends Bike{
int model()
//long is compatible with int since there is no loss of precision
{
return 1234;
}
public static void main(String args[]){
Bike obj = new Honda4();
System.out.println(obj.model());
}
}
你能说出一个与 int
不兼容的 return 类型吗?
short
。这可能会导致精度损失。
我是 Java 的新手,所以我正在阅读 Java Head First。我已经看到当你有一个带有抽象方法的抽象 class 时,你应该在一个具体的 class 中覆盖这些抽象方法,这意味着 "create a non-abstract method in your class with the same method signature (name and arguments) and a return type that is compatible with the declared return type of the abstract method."
我可以清楚地理解关于具有相同签名(名称和参数)的第一部分,但我想对 a return 类型有一个明确的解释与抽象方法声明的 return 类型兼容。
兼容类型到底是什么意思?有人可以举个例子吗?好像return类型应该是抽象方法中定义的return类型的class或者subclass?
覆盖方法返回的类型必须相同,或者必须是基方法返回类型的子类或子接口。
简而言之:它必须尊重基方法的契约。如果基本方法说:"I return a Fruit",那么覆盖方法可以说 "I return a Fruit",但它也可以说 "I return a Banana"。
没关系,因为香蕉是水果。任何调用该方法并获得香蕉的人都很高兴:期望得到一个水果,并且收到了一个水果。
虽然还车是不正确的,因为当你要水果时,得到车是不可接受的。
这个的技术术语是 covariant return type。请注意,即使基数 class/method 不是抽象的,这条规则也是正确的。
如果您的 return 类型是 A
,那么您还可以 return 来自 A
的任何 class B
的对象。所有这些都是兼容类型。
您的 return 类型也可以是接口。在这种情况下,您可以 return 任何实现此接口的对象。
这意味着您对抽象方法的实现必须 return 与抽象 class 中定义的对象相同或派生 class 例子: 你有一个带有抽象方法的抽象类
abstract class AbstractTest{
abstract Date getInitialTime();
}
那么你有一个测试class。
class Test extends AbstractTest{
...
Date getInitialTime(){
return new Date();
}
}
您重写了 getInitialTime
方法,该方法的 return 必须是 class 日期或 [=13] 的超class 的对象=]
如果你 return 其他东西,那么你就是在破坏你的 class return 方法和 os 父方法之间的契约 class returns...
考虑下面的例子。抽象函数model()
的return类型是int
。
abstract class Bike{
abstract short model();
}
- 当您在扩展抽象 class
Bike
的具体 class 中重新定义此方法时。具体 class 应该有一个具有相同方法签名和兼容 return 类型的方法model()
。
One return type is compatible with another, if it doesn't lead to loss of precision.
One return type compatible with
short
isint
.
class Honda4 extends Bike{
int model()
//long is compatible with int since there is no loss of precision
{
return 1234;
}
public static void main(String args[]){
Bike obj = new Honda4();
System.out.println(obj.model());
}
}
你能说出一个与 int
不兼容的 return 类型吗?
short
。这可能会导致精度损失。