java - 使用泛型方法创建不可变的基础

标签 java android inheritance immutability

我有一个这样的基类:

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/

相关文章:

Java正则表达式仅字符串开头

java - Bukkit 事件多次触发

java - 为什么 Figwheel 不将编译后的应用程序传递到浏览器?

java - Java中有类似JavaScript的 "prototype"的东西吗?

java - 继承时代码重复

c++ - 创建父类(super class)的指针数组以堆分配继承类

java - 在 JavaFX 中显示复杂的印度孟加拉语字体

java - libgdx - Actor 输入事件未触发

android - Android 可以处理不规则形状的事件吗?

android - GSON可以处理静态私有(private)嵌套类吗