我正在编写一个程序来模拟人工神经网络。我已经设置了以下类和接口:
public interface Neuron
{
}
// Input neuron
public class INeuron implements Neuron
{
}
// Output and hidden neuron
public class ONeuron implements Neuron
{
}
public interface Layer
{
public ArrayList<Neuron> getNeurons();
}
// Input layer
public class ILayer implements Layer
{
ArrayList<INeuron> neurons = new ArrayList<INeuron>();
public ArrayList<Neuron> getNeurons()
{
return neurons;
}
// other stuff appropriate to the input layer
}
编译器报错:"无法将ArrayList<INeuron>转换为ArrayList<Neuron>。"
我尝试过调整代码,比如:ArrayList<Neuron> neurons = new ArrayList<INeuron>()
。但是这似乎只是把同样的错误转移到类的其他部分。
我不明白为什么INeuron不能隐式转换为Neuron,因为INeuron是Neuron的子类型。
Generic<SubType>
和Generic<Type>
之间有很好的理由不相同。 - trutheality