【发布时间】:2021-08-06 18:16:50
【问题描述】:
我想要达到的目标:
我正在尝试编写一个抽象类,它是所有子类都应具有的数据类型和行为的蓝图。但是,这些强制方法的返回类型将取决于子类本身。
我的尝试:
我在 cmets 中被告知我的许多封闭问题之一,我在这里所做的是重载我父母的方法而不是覆盖它们。我理解这是因为我更改了方法签名,@Override 要求签名相同,但实现可以不同。
public abstract class BaseMatrix {
protected int[] shape;
protected int nrows;
protected int ncols;
public BaseMatrix(int rows, int cols){
this.nrows = rows;
this.ncols = cols;
this.shape = new int[]{nrows, ncols};
}
// ** here is the method I want to implement/override **
public abstract BaseMatrix mmul(BaseMatrix other);
我最初尝试使用泛型来解决这个问题,但发现我对它们的理解不够好,无法以这种方式实施解决方案。因此,我采用了在两个类似问题中找到的建议:https://stackoverflow.com/a/20638886/3696204 和 Proper use of generics in abstract java class?
public class ND4JDenseMatrix extends BaseMatrix{
private INDArray data;
public ND4JDenseMatrix(int rows, int cols) {
super(rows, cols);
this.data = Nd4j.zeros(this.shape);
}
// ** Here is my attempt at implementing the abstract method. **
@Override
public ND4JDenseMatrix mmul(ND4JDenseMatrix other) {
INDArray product = this.data.mmul(other.data);
ND4JDenseMatrix result = new ND4JDenseMatrix(this.nrows, this.ncols);
result.setData(product);
return this;
}
}
这会导致@Override 处出现警告:
Method does not override method from its superclass
但是,如果我在子类中使用相同的方法签名:
public class ND4JDenseMatrix extends BaseMatrix{
public BaseMatrix mmul(BaseMatrix other) { ...implementation...}
}
然后有人可以传递BaseMatrix 类mmul() 的任何子级,这几乎肯定会中断。
我的问题
鉴于我知道我正在重载而不是覆盖,我如何才能实现我的问题第一部分中描述的强制将类型传递给子类方法的功能?
【问题讨论】:
-
A
ND4JDenseMatrixis aBaseMatrix,因此您可以将方法签名保留为public BaseMatrix mmul(BaseMatrix),仍然传入并返回ND4JDenseMatrix。 -
@azurefrog 但是这不会允许用户传入
BaseMatrix类的任何孩子吗?因为如果发生这种情况,结果可能会失败......
标签: java inheritance abstract-class