简体   繁体   English

当子类对象存储在超类数组中时,如何调用子类方法?

[英]How to call subclass methods when subclass objects are stored in superclass array?

{
    Ship ships = new Ship();
    CargoShip cargoShips = new CargoShip();
    CruiseShip cruiseShips = new CruiseShip();

    Ship[] allShips = {ships, cargoShips, cruiseShips};

    allShips[0].setShipName("Boom");
    allShips[0].setYearBuilt("1900");
    allShips[1].setShipName("Bang");
    allShips[1].setCargoCapaicty(200);
    allShips[2].setShipName("Bam");
    allShips[2].setMaxPassengers(500);


    for (int i = 0; i < allShips.length; i++)
    {
        System.out.println(allShips[i]);
    }
}

So the Ship class is the super class while CargoShip and CruiseShip both extend the Ship class. 因此,Ship类是超类,而CargoShip和CruiseShip都扩展了Ship类。 I've stored the 3 objects into a Ship array. 我已经将3个对象存储到Ship数组中。 setCargoCapacity and setMaxPassengers are methods that only appear in the subclasses. setCargoCapacity和setMaxPassengers是仅出现在子类中的方法。 For some reason I cannot access them. 由于某些原因,我无法访问它们。 I can't figure out how to make it so that I can also access the subclass methods. 我不知道如何做到这一点,以便我也可以访问子类方法。

You can't call setCargoCapacity of a Ship you picked out of a Ship[] because that ship might not be a CargoShip . 您不能调用从Ship[]拾取的Ship setCargoCapacity ,因为该船可能不是CargoShip You must either provide some method in the Ship class (and therefore for all Ships) that does what you need it to do, or check whether ship instanceof CargoShip , and if so you can cast it ( CargoShip cargoShip = (CargoShip)ship ) and call the setCargoCapacity of that. 您必须在Ship类中(因此对于所有Ships)提供某种您需要做的方法,或者检查是否ship instanceof CargoShip ,如果可以,则可以将其CargoShip cargoShip = (CargoShip)shipCargoShip cargoShip = (CargoShip)ship )和调用它的setCargoCapacity

You could initialize your objects before storing them in the array: 您可以将对象存储在数组中之前对其进行初始化:

Ship ships = new Ship();
ships.setShipName("Boom");
ships.setYearBuilt("1900");

CargoShip cargoShips = new CargoShip();
cargoShips.setShipName("Bang");
cargoShips.setCargoCapaicty(200);

CruiseShip cruiseShips = new CruiseShip();
cruiseShips.setShipName("Bam");
cruiseShips.setMaxPassengers(500);

Ship[] allShips = {ships, cargoShips, cruiseShips};

You cant do it. 你做不到。 In java reference variables have two types: reference type and actual object type. 在Java中,引用变量有两种类型:引用类型和实际对象类型。 So, when you write something like Object o = "12345" , o has type Object (not String ), and you cant work with o like with string without typecasting: o = ((String)o).substring(1); 因此,当您编写类似Object o = "12345"o类型为Object (not String ),并且您不能在不进行类型转换的情况下使用带字符串的o工作: o = ((String)o).substring(1); .

In your case all references in your array allShips have type Ship . 在您的情况下,数组allShips所有引用的类型都是Ship If you know that actual type of some array element is a Ship subclass, you can cast actual type like above. 如果您知道某个数组元素的实际类型是Ship子类,则可以像上面一样强制转换实际类型。 But it's a bad practice and shouldn't be used in real world. 但这是一个不好的做法,不应在现实世界中使用。

allShips[0].setShipName("Boom");
allShips[0].setYearBuilt("1900");
allShips[1].setShipName("Bang");
((CargoShip)allShips[1]).setCargoCapaicty(200);
allShips[2].setShipName("Bam");
((CruiseShip)allShips[2]).setMaxPassengers(500);

try this. 尝试这个。 It will work. 它会工作。

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

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