简体   繁体   English

Java - 理解继承

[英]Java - Understanding Inheritance

I'm new to Java and have been taking a course for a couple of weeks now and have been asked to complete a program with the following information:我是 Java 新手,已经参加了几周的课程,并被要求完成一个包含以下信息的程序:

  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.显示船舶名称和建造年份的 toString 方法。

  2. Design a CruiseShip sub class that extends the Ship class.设计一个 CruiseShip 子类来扩展 Ship 类。 The CruiseShip class should have the following: CruiseShip 类应具有以下内容:

    • 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.覆盖基类中 toString 方法的 toString 方法。 The CruiseShip class's toString method should also include the max passenger limit. CruiseShip 类的 toString 方法还应包括最大乘客限制。

  3. Design a CargoShip class that extends the Ship class.设计一个扩展 Ship 类的 CargoShip 类。 The CargoShip class should have the following: CargoShip 类应具有以下内容:

    • 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.覆盖基类中 toString 方法的 toString 方法。 The CargoShip class's toString method should also include the cargo tonnage. CargoShip 类的 toString 方法还应包括货物吨位。

  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.在适当的类中包含一个 equals 方法来测试两艘船是否相等 - 如果它们具有相同的名称并在同一年建造,则应将它们视为相等。

  5. Demonstrate the classes in a program (ShipTester) that has an array of Ship objects (at least 5 of them).演示具有 Ship 对象数组(至少 5 个)的程序 (ShipTester) 中的类。 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.将各种 Ship、CruiseShip 和 CargoShip 对象分配给数组元素(您可以对对象和数据进行硬编码)并打印出初始船舶配置。 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).证明您可以在船舶样本上同时使用访问器和修改器方法(同样,如果您愿意,可以在这里对方法参数进行硬编码)。

I understand the basis of what the question is asking and I have done the following:我了解问题的基础,并且我已完成以下操作:

Ship.java 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游轮.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货船.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 + "]";

    }

}

and in my tester file - ShipTester.java并在我的测试文件中 - ShipTester.java

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

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

    }
}

I don't know what to put in the main method... I do know I have to create an array of ships but i don't know how to do that with the cargo ship, cruise ship and so on...我不知道在 main 方法中放什么......我知道我必须创建一系列船,但我不知道如何用货船、游轮等来做到这一点......

Output that i'm supposed to have:我应该有的输出:

Displaying Ships: Ship[ Name: Queen Annes Revenge Year built: 1701]显示船舶:船舶[名称:安妮女王复仇号建造年份:1701]

Cruise Ship[ Ship[ Name: USS Enterprise Year built: 2245], Passengers: 2400 ]游轮[船[名称:USS Enterprise 建造年份:2245],乘客:2400]

Cargo Ship[ Ship[ Name: Black Pearl Year built: 1699], Tonnage: 50000 ]货船[船[名称:黑珍珠号建造年份:1699],吨位:50000]

Cruise Ship[ Ship[ Name: USS Voyager Year built: 2371], Passengers: 2800 ]游轮[船[名称:航海者号航母建造年份:2371],乘客:2800]

Cargo Ship[ Ship[ Name: The Victory Year built: 1790], Tonnage: 33100 ]货船[船[名称:胜利年份:1790年],吨位:33100]

Extending onto what Hovercraft said but you may want to use an ArrayList for arrays of objects.扩展到 Hovercraft 所说的内容,但您可能希望将 ArrayList 用于对象数组。

Creating an Arraylist of Objects 创建对象的 Arraylist

Example例子

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

etc.等等。

[edit] forgot you needed to print it too [编辑] 忘了你也需要打印它

You use a simple for loop to print, example:您使用一个简单的 for 循环来打印,例如:

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

note - this is not the exact answer I dont want jump over your learning process.注意 - 这不是我不想跳过您的学习过程的确切答案。 But this should at least give you an idea of a path you could take...但这至少应该让您了解可以采取的道路......

Ship is the "base" parent class which puts all the other classes into a common family. Ship是“基础”父类,它将所有其他类放入一个公共系列中。 This means that CruiseShip and CargoShip can actually masquerade as Ship .这意味着CruiseShipCargoShip实际上可以伪装成Ship This is the corner stone of Polymorphism and one of the features that makes Object Oriented Program so powerful.这是多态的基石,也是使面向对象程序如此强大的特性之一。

Based on your example code, you would simply need to create an array of type Ship , for example...根据您的示例代码,您只需要创建一个Ship类型的数组,例如...

Ship[] ships = new Ship[5];

You would then simply need to fill this arrays with an assortment of ships...然后你只需要用各种各样的船来填充这个数组......

ships[0] = new CargoShip("Lolli Pop", 1965, 1990);
ships[1] = new CruiseShip("Love Boat", 1980, 8);

What this means is, that each element "acts" as a Ship , since CargoShip and CruiseShip inherited from Ship , this gives them the ability to do so, because they are "ships"这意味着,每个元素都“充当”一个Ship ,因为CargoShipCruiseShip继承自Ship ,这使它们能够这样做,因为它们是“船”

Because the display information for Ship is defined by it's toString method you can simply use System.out.println(instanceOfShip) to print it.因为Ship的显示信息是由它的toString方法定义的,所以您可以简单地使用System.out.println(instanceOfShip)来打印它。 For example...例如...

for (Ship ship : ships) {
    System.out.println(ship);
}

Take a closer look at Inheritance and Arrays for more details仔细查看继承数组以获取更多详细信息

It might seem confusing and daunting, but once you understand the basics of inheritance, it will become very obvious and very powerful这可能看起来令人困惑和令人生畏,但一旦你了解了继承的基础知识,它就会变得非常明显和非常强大

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

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