Java-理解继承

Java-理解继承,java,object,inheritance,Java,Object,Inheritance,我是Java新手,已经参加了几周的课程,被要求完成一个包含以下信息的课程: 设计具有以下数据字段的船舶等级: 船名的数据字段(字符串) 船舶建造年份的数据字段(整数) 构造函数和适当的访问器和变异器 显示船舶名称和建造年份的toString方法 设计一个CruiseShip子类来扩展船舶类。CruiseShip类应具有以下特性: 最大乘客数的额外数据字段(整数) 构造函数和适当的访问器和变异器 重写基类中的toString方法的toString方法。CruiseShip类的toString

我是Java新手,已经参加了几周的课程,被要求完成一个包含以下信息的课程:

  • 设计具有以下数据字段的船舶等级:

    • 船名的数据字段(字符串)

    • 船舶建造年份的数据字段(整数)

    • 构造函数和适当的访问器和变异器

    • 显示船舶名称和建造年份的toString方法

  • 设计一个CruiseShip子类来扩展船舶类。CruiseShip类应具有以下特性:

    • 最大乘客数的额外数据字段(整数)

    • 构造函数和适当的访问器和变异器

    • 重写基类中的toString方法的toString方法。CruiseShip类的toString方法还应该包括 最高乘客限额

  • 设计扩展Ship类的CargoShip类。货船类别应具有以下内容:

    • 以吨位为单位的货物容量的额外数据字段(整数)

    • 构造函数和适当的访问器和变异器

    • 重写基类中的toString方法的toString方法。CargoShip类的toString方法还应包括 货物吨位

  • 在适当的类别中,包括一个equals方法来测试两艘船是否相等——如果它们具有相同的 同一个名字和同一年建造的

  • 在程序(ShipTester)中演示具有Ship对象数组(至少5个)的类。分配各种船舶、巡洋舰、, 和CargoShip对象到数组元素(您可以硬编码 对象和数据),并打印出初始船舶配置。显示 您可以在采样上同时使用访问器和mutator方法 (同样,您可以在这里硬编码方法参数,如果 你喜欢什么

  • 我理解问题的基础,我已经做了以下工作:

    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...
    
        }
    }
    
    我不知道在主方法中放什么。。。我知道我必须创建一系列的船只,但我不知道如何使用货船、游轮等等

    我应该拥有的输出:

    展示船舶: 船[名称:安妮女王复仇号建造年份:1701]

    游轮[船[名称:USS企业号制造年份:2245],乘客:2400]

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

    邮轮[船名:美国航海家号,建造年份:2371],乘客:2800]


    货船[船名:胜利年建造:1790],吨位:33100]

    扩展到气垫船上,但您可能希望使用ArrayList来显示对象阵列

    范例

    ArrayList<Ship> myArray = new ArrayList();
    myArray.add(new CargoShip("TheKing",1990,10000));
    myArray.add(new CruiseShip("Princess",2000,5000));
    
    ArrayList myArray=new ArrayList();
    myArray.add(新货船(“TheKing”,199010000));
    myArray.add(新邮轮号(“公主号”,20005000));
    
    等等

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

    使用简单的for循环进行打印,例如:

    for (int i = 0; i < myArray.size(); i++) {
      System.out.println(myArray.get(i).toString());
    }
    
    for(int i=0;i
    注意-这不是确切的答案,我不想跳过你的学习过程。但这至少应该给你一个可以选择的路径的想法…

    Ship
    是“基本”父类,它将所有其他类放入一个公共族中。这意味着
    CruiseShip
    CargoShip
    实际上可以伪装成
    Ship
    。这是面向对象程序的基石,也是使面向对象程序如此强大的特性之一

    根据示例代码,您只需创建一个类型为
    Ship
    的数组,例如

    Ship[] ships = new Ship[5];
    
    for (Ship ship : ships) {
        System.out.println(ship);
    }
    
    然后,您只需使用各种船舶填充此阵列

    ships[0] = new CargoShip("Lolli Pop", 1965, 1990);
    ships[1] = new CruiseShip("Love Boat", 1980, 8);
    
    这意味着,每个元素“充当”一艘
    船舶
    ,因为
    货船
    巡洋舰
    继承自
    船舶
    ,这赋予了它们这样做的能力,因为它们是“船舶”

    因为
    Ship
    的显示信息是由它的
    toString
    方法定义的,所以您可以简单地使用
    System.out.println(instanceOfShip)
    来打印它。例如

    Ship[] ships = new Ship[5];
    
    for (Ship ship : ships) {
        System.out.println(ship);
    }
    
    请仔细查看和了解更多详细信息


    这看起来可能令人困惑和畏惧,但一旦你理解了继承的基础知识,它就会变得非常明显和强大。提示,你刚刚说了答案:……我必须创建一个船舶数组……
    Ship[]ships=newship[5]然后分配
    装运[0]=..
    。我相信,你可以做到这一点,试一试,看看会发生什么。我看到未来的单词
    new
    。顺便说一句,当重写
    等于
    时,你应该总是重写
    hashCode
    ,否则像
    HashMap
    这样的容器会出现故障。通常最好不要使用
    equals
    (使用默认的
    =
    实现),直到您编写了它的实际实现。@ElliottFrisch是的,但这不是一个很好的默认值,依我看。
    “Unnamed”
    或类似的东西会减少误导。1)他的说明提到创建数组,不是像ArrayList这样的集合。2)
    println(…)
    语句中不需要调用
    toString()
    ,因为它是默认调用的。后者是一个次要的狡辩,但我的第一句话更重要。谢谢!我能得到如何使用equals方法的提示吗?我在想一些类似于
    公共布尔等于(ship[0],ship[1])
    的东西