我有一个这样的基类:
public abstract class BasePiece extends Serialisable {
public final Position[] shape;
public final Position position;
public abstract Position[] getInitialShape();
public BasePiece() {
position = new Position(0, 0);
shape = getInitialShape();
}
public BasePiece(Position pos, Position[] initialShape) {
position = pos;
shape = initialShape;
}
public BasePiece Moved(Position offset) {
return BasePiece(position.add(offset), shape);
}
public BasePiece Rotated() {
return BasePiece(position, shape.Rotated());
}
}
但是我希望 Moved 和 Rotated 返回继承此类的类的实例。我对 Java 相当陌生,并且有一些 C# 经验,我尝试执行以下操作:
public <T extends BasePiece> T Moved(Position offset) {
return T(position.add(offset), shape);
}
public <T extends BasePiece> T Rotated() {
return T(position, shape.Rotated());
}
有什么办法可以做到这一点吗?我最初尝试解决这个问题是使形状和位置不再是最终的,并使用 Moved 和 Rotated Move 和 Rotate 方法来改变这种状态。我真的想让对象不可变,因为这将使我的应用程序的其余部分更易于管理
最佳答案
我认为这与泛型关系不大。您可以将方法声明保留为:
public BasePiece Moved(Position offset) {
return BasePiece(position.add(offset), shape);
}
并在扩展它的类中:
class ExtendPiece extends BasePiece {
@Override
public BasePiece Moved(Position offset) {
return ExtendPiece(position.add(offset), shape);
}
}
如 ExtendPiece
可以向下转换为 BasePiece
。
但是当然你必须自己转换它:
ExtendPiece e = (ExtendPiece) obj.Moved(..);
编辑:
一个快速的解决方法是使用所谓的双重调度:
让每个子类实现方法newInstance()
,该方法在基类中是抽象的。在扩展部分中:
public BaseClass newInstance(Position pos, Position[] initialShape){
return new ExtendedPiece(pos, initialShape);
}
抽象方法的代码为:
public BasePiece Moved(Position offset) {
return this.newInstance(position.add(offset), shape);
}
关于java - 使用泛型方法创建不可变的基础,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21664959/