我有一个基类Building和子类HouseSchool。我已经通过以下方式声明了array对象的Building。我不确定如何为每个数组元素(运行时类型)分配子类。

Building[] House = new Building[3];
Building[] School = new School[2];

最佳答案

您可以使用instanceof确定子类

if (building instanceof House) {
    House[0] = building;
}

10-06 02:00