【发布时间】:2012-07-25 05:49:27
【问题描述】:
我有一个带有构造函数的类 Vector
Vector(int dimension) // creates a vector of size dimension
我有一个扩展 Vector 类的 Neuron 类
public class Neuron extends Vector {
public Neuron(int dimension, ... other parameters in here ...) {
super(dimension);
// other assignments below here ...
}
}
我希望能够为 Neuron 类中的 Vector 分配对另一个 Vector 的引用。类似于
public Neuron(Vector v, ... other parameters in here ...) {
super = v;
// other assignments below here ...
}
当然,我不能这样做。有什么解决办法吗?即使我无法在 Neuron 类的构造函数中执行此操作,也可能没问题。
【问题讨论】:
-
想要这样做通常表明您应该使用composition instead of inheritance
-
除非必须,否则我不会使用 Vector。我会使用 ArrayList 而不是子类。最好根据需要使用委托。
-
Vector 是我自己的类,它模仿数学向量的行为。我应该说清楚,我不是在这里谈论 java.util Vector 类。然而,这更像是一个关于分配对超类的引用的一般问题。
标签: java inheritance reference superclass