你能用 baseclass 的实例初始化派生的 class 吗?

Can you initialize a derived class with an instance of a baseclass

我的意思是一个基class,它本身并没有使用派生class的实例初始化。即假设它不是抽象的 class.

class GeomObj{
  Colour x;
}

class Triangle extends GeomObj{
  largestAngle y;
}

GeomObj u;

//now is the following allowed?Taking into account that u was not initialized using an instance of Triangle in the first place

Triangle v = (Triangle)u;

否,因为 GeomObj 不是三角形。但反之亦然:

Triangle u;
GeomObj v = (GeomObj)u;

此外,你可以说每个Triangle都是GeomObj,但并不是每个GeomObj都是Triangle。所以编译器不允许你这样做。