简体   繁体   English

抽象超类的子类的方法可以返回超类的超类返回类型吗?

[英]Can a method of a subclass of an abstract super class return a super class of the super class return type?

I have an abstract super class that implements an interface: 我有一个实现接口的抽象超类:

public abstract class FooMatrix implements Matrix {
    public Vector multiply(Vector vec) {
        // Code for Matrix * Vector
    }
}

public interface Matrix {
    public Vector multiply(Vector vec);
}

and then I have a subclass that extends the super class and implements a second interface that represents a mathematical subclass of the mathematical class represented by the first interface: 然后,我有一个子类,该子类扩展了超类并实现了第二个接口,该接口代表由第一个接口表示的数学类的数学子类:

public interface Vector {
    // Methods
}

public class FooVector extends FooMatrix implements Vector {
    @Override
    public Matrix multiply(Vector rightVec) {
        // Code for Vector * Vector
    }
}

So the subclass is returning a super class of the return type of the abstract super class. 因此,子类将返回抽象超类的返回类型的超类。 This isn't working, and I want to know how I can make it work. 这不起作用,我想知道如何使它起作用。

In mathematics, vectors are a subclass of matrices. 在数学中,向量是矩阵的子类。 A matrix times a matrix yields another matrix, in general. 通常,一个矩阵乘以一个矩阵得出另一个矩阵。 If the second matrix is a vector, the result is a vector. 如果第二矩阵是向量,则结果是向量。 If both matrices are vectors, the result is a matrix or a scalar for column vector times row vector or row vector times column vector, respectively. 如果两个矩阵都是向量,则结果分别是列向量乘以行向量或行向量乘以列向量的矩阵或标量。 This assumes the dimensions are compatible. 假设尺寸是兼容的。

I would like to represent this behaviour with Java classes in a way that respects mathematics and gives the most specific return types possible. 我想用尊重数学的方式并给出最具体的返回类型的方式用Java类表示这种行为。 In other words, I want fooMatrix.multiply(Vector vec) to return Vector , and not just Matrix , but I want fooVector.multiply(Vector rightVec) to return Matrix and not Vector (which would be incorrect.) I can deal with a scalar as a 1x1 Matrix with another Vector method inner that calls multiply(Vector rightVec) and then returns the single element of the 1x1 return Matrix as a scalar. 换句话说,我希望fooMatrix.multiply(Vector vec)返回Vector ,而不仅仅是Matrix ,但是我想要fooVector.multiply(Vector rightVec)返回Matrix而不是Vector (那是不正确的)。我可以处理标量为1x1 Matrixinner还有另一个Vector方法,该方法调用multiply(Vector rightVec) ,然后将1x1的单个元素作为标量返回Matrix

I have found questions regarding covariant return types, but nothing like this. 我发现了有关协变返回类型的问题,但没有这样的问题。

Thanks! 谢谢!

Because Matrix and Vector are in two different hierarchies. 因为MatrixVector在两个不同的层次结构中。 You cant use Matrix in place of Vector as a return type. 您不能使用Matrix代替Vector作为返回类型。
If you are not sure of the return type whether its going to be of type Matrix or Vector , you can new interface which is extended by Matrix and Vector and use it as return type. 如果不确定返回类型是Matrix还是Vector ,则可以新建由MatrixVector扩展的接口,并将其用作返回类型。
Somewhat like 有点像

public interface MatrixOrVector {
    // This can be used as return type
}   

change Matrix to Matrix更改为

public interface Matrix extends MatrixOrVector{
   // Methods...
}

same with Vector 与Vector相同

public interface Vector extends MatrixOrVector{
    // Methods ....
}

If you use MatrixOrVector as return type, you can return object which is of type Matrix or Vector . 如果使用MatrixOrVector作为返回类型,则可以返回MatrixVector类型的对象。

Also just FYI FooMatrix is not a abstract class because you are missing abstract there. 同样,FYI FooMatrix不是抽象类,因为您在那里缺少abstract read more about abstract http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html 阅读有关抽象的更多信息http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM