pse*_*giu 2 java types overloading return
我有这样一个类树:
master class abstract class Cell
AvCell extends Cell
FCell extends Cell
Run Code Online (Sandbox Code Playgroud)
我有一个抽象方法getValue()
在Cell
难道posibble使方法getValue()
返回int
的AvCell
和String
为FCell
?我可以使用泛型int
String
吗?
谢谢!
Yan*_*eve 14
你可以使用泛型并声明一个Cell<T>
.然后就getValue()
回来吧T
abstract class Cell<T> {
abstract T getValue();
}
Run Code Online (Sandbox Code Playgroud)
现在:
class AvCell extends Cell<Integer> {
}
Run Code Online (Sandbox Code Playgroud)
和
class FCell extends Cell<String> {
}
Run Code Online (Sandbox Code Playgroud)
您可以执行以下操作:
abstract class Cell<T> {
public abstract T getValue();
}
class AvCell extends Cell<Integer> {
public Integer getValue() {
return 0;
}
}
class FCell extends Cell<String> {
public String getValue() {
return "foo";
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5657 次 |
最近记录: |