参数化超类方法和子类中的非参数化(具体类型)方法
Parameterizing superclass method and non-parameterized (concrete type) method in subclass
我有一个抽象超类 ValueType
和两个非抽象子类 IntType
和 DoubleType
。如何在超类方法中定义类型,如下所示
public abstract class ValueType {
public abstract <T extends ValueType> T add(T other);
}
我允许在子类方法
中编写具体类型(例如 public IntType add(IntType other)
或 DoubleType add(DoubleType other)
)
public class IntType extends ValueType {
private int val;
public IntType(int val){
this.val = val;
}
public int getVal(){
return this.val;
}
@Override
public IntType add(IntType other) {
return new IntType(this.val + other.getVal());
}
public class DoubleType extends ValueType {
private double val;
public DoubleType(double val){
this.val = val;
}
public double getVal(){
return this.val;
}
@Override
public DoubleType add(DoubleType other) {
return new DoubleType(this.val + other.getVal());
}
在上面的例子中,子类方法没有覆盖超类方法。
使抽象 class 泛型本身,而不仅仅是它的方法。
将泛型类型 <T extends ValueType>
提升为 class 泛型参数。请注意,通过此操作,class ValueType
也变得通用,因此 <T extends ValueType<T>>
取代原始类型:
public abstract class ValueType<T extends ValueType<T>> {
public abstract T add(T other);
}
然后具体子 class 从泛型 class 扩展并强制重写抽象方法(私有字段、getter 和 setter 省略):
public class IntType extends ValueType<IntType> {
@Override
public IntType add(IntType other) {
return new IntType(this.val + other.getVal());
}
}
public class DoubleType extends ValueType<DoubleType> {
@Override
public DoubleType add(DoubleType other) {
return new DoubleType(this.val + other.getVal());
}
}
顺便说一句,你有两个错别字:
return new IntType(this.value + other.getVal());
应该使用 this.val
.
return new DoubleType(this.value + other.getVal());
也应该使用 this.val
。
我有一个抽象超类 ValueType
和两个非抽象子类 IntType
和 DoubleType
。如何在超类方法中定义类型,如下所示
public abstract class ValueType {
public abstract <T extends ValueType> T add(T other);
}
我允许在子类方法
中编写具体类型(例如public IntType add(IntType other)
或 DoubleType add(DoubleType other)
)
public class IntType extends ValueType {
private int val;
public IntType(int val){
this.val = val;
}
public int getVal(){
return this.val;
}
@Override
public IntType add(IntType other) {
return new IntType(this.val + other.getVal());
}
public class DoubleType extends ValueType {
private double val;
public DoubleType(double val){
this.val = val;
}
public double getVal(){
return this.val;
}
@Override
public DoubleType add(DoubleType other) {
return new DoubleType(this.val + other.getVal());
}
在上面的例子中,子类方法没有覆盖超类方法。
使抽象 class 泛型本身,而不仅仅是它的方法。
将泛型类型 <T extends ValueType>
提升为 class 泛型参数。请注意,通过此操作,class ValueType
也变得通用,因此 <T extends ValueType<T>>
取代原始类型:
public abstract class ValueType<T extends ValueType<T>> {
public abstract T add(T other);
}
然后具体子 class 从泛型 class 扩展并强制重写抽象方法(私有字段、getter 和 setter 省略):
public class IntType extends ValueType<IntType> {
@Override
public IntType add(IntType other) {
return new IntType(this.val + other.getVal());
}
}
public class DoubleType extends ValueType<DoubleType> {
@Override
public DoubleType add(DoubleType other) {
return new DoubleType(this.val + other.getVal());
}
}
顺便说一句,你有两个错别字:
return new IntType(this.value + other.getVal());
应该使用this.val
.return new DoubleType(this.value + other.getVal());
也应该使用this.val
。