Java - 理解继承

标签 java object inheritance

我是 Java 新手,现在已经参加类(class)几周了,并被要求使用以下信息完成一个程序:

  1. Design a ship class that has the following data fields:

    • A data field for the name of the ship (a string).

    • A data field for the year that the ship was built (an int).

    • A constructor and appropriate accessors and mutators.

    • A toString method that displays the ship’s name and the year it was built.

  2. Design a CruiseShip sub class that extends the Ship class. The CruiseShip class should have the following:

    • An extra data field for the maximum number of passengers (an int).

    • A constructor and appropriate accessors and mutators.

    • A toString method that overrides the toString method in the base class. The CruiseShip class's toString method should also include the max passenger limit.

  3. Design a CargoShip class that extends the Ship class. The CargoShip class should have the following:

    • An extra data field for the cargo capacity in tonnage (an int).

    • A constructor and appropriate accessors and mutators.

    • A toString method that overrides the toString method in the base class. The CargoShip class's toString method should also include the cargo tonnage.

  4. In the appropriate class include an equals method to test if two ships are equal - they should be considered equal if they have the same name and were built in the same year.

  5. Demonstrate the classes in a program (ShipTester) that has an array of Ship objects (at least 5 of them). Assign various Ship, CruiseShip, and CargoShip objects to the array elements (you can hard-code the objects and data) and print out the initial ship configurations. Show that you can use both the accessor and mutator methods on a sampling of the ships (again, you can hard-code the method arguments here if you like).

我了解问题所问的基础,并且我已执行以下操作:

Ship.java

public class Ship {

    private String name;
    private int yearBuilt;

    public Ship(String name, int yearBuilt) {

        this.name = name;
        this.yearBuilt = yearBuilt;

    }

    public String returnName() {

        return this.name;

    }

    public int returnYear() {

        return this.yearBuilt;

    }

    public boolean equals(Ship other) {

        return false;

    }

    public String toString() {

        return "[Name: " + this.name + ". Year built: " + this.yearBuilt + "]";

    }
}

CruiseShip.java

public class CruiseShip extends Ship {

    private int maxPassengers;

    public CruiseShip() {

        super("USS Enterprise", 2245);

        this.maxPassengers = 2400;

    }

    public CruiseShip(String name, int yearBuilt, int maxPassengers) {

        super(name, yearBuilt);
        this.maxPassengers = maxPassengers;

    }

    public String toString() {

        return "Cruise Ship" + super.toString() + ", Passengers: " + this.maxPassengers + "]";

    }

}

CargoShip.java

public class CargoShip extends Ship {

    private int cargoCapacity;

    public CargoShip() {

        super("Black Pearl", 1699);

        this.cargoCapacity = 50000;

    }

    public CargoShip(String name, int yearBuilt, int cargoCapacity) {

        super(name, yearBuilt);
        this.cargoCapacity = cargoCapacity;

    }

    public String toString() {

        return "Cargo Ship" + super.toString() + ", Tonnage: " + this.cargoCapacity + "]";

    }

}

在我的测试器文件中 - ShipTester.java

public class ShipTester {
    public static void main(String []args) {

        //I don't know what to put over here...

    }
}

我不知道要在主方法中放入什么...我确实知道我必须创建一系列船只,但我不知道如何使用货船、游轮等来创建船只。 ..

我应该有的输出:

显示船舶: 船[名称:安妮女王复仇号 build 年份:1701]

游轮[船[名称:USS Enterprise build 年份:2245],乘客:2400]

货船[船[名称:黑珍珠号 build 年份:1699年],吨位:50000]

游轮[船[名称:USS Voyager build 年份:2371],乘客:2800]

货船[船[名称:胜利号 build 年份:1790年],吨位:33100]

最佳答案

扩展 Hovercraft 所说的内容,但您可能希望使用 ArrayList 来存储对象数组。

Creating an Arraylist of Objects

示例

ArrayList<Ship> myArray = new ArrayList();
myArray.add(new CargoShip("TheKing",1990,10000));
myArray.add(new CruiseShip("Princess",2000,5000));

等等

[编辑]忘记你也需要打印它

您使用一个简单的 for 循环来打印,例如:

for (int i = 0; i < myArray.size(); i++) {
  System.out.println(myArray.get(i).toString());
}

注意 - 这不是确切的答案,我不想跳过您的学习过程。但这至少应该让您了解可以采取的路径...

关于Java - 理解继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21445898/

相关文章:

Java:J进度条

java - 在 Java 中添加实例变量后静态方法/变量的行为发生变化

java - JAX-RS 中使用 @Required 注解的必需 @QueryParam 在 ContainerRequestFilter 中不起作用

java - Scala GUI - 向文本区域添加滚动条

java - 从 java 中调用 javascript 函数

java - Java 构造函数的 Scala 继承

c++ - 错误C2011 : 'class type redefinition - Basic Inheritance

javascript - 如何通过key获取javascript对象的值

powershell - 检查 PowerShell 对象是否存在的最佳方法?

Java字符串对象的创建