关于多态课后习题
大家谁完成率本节作业题?我都没搞懂意思,借鉴一下,谢谢
大家谁完成率本节作业题?我都没搞懂意思,借鉴一下,谢谢
2016-05-05
建立一个父类,让子类继承其所有方法,获取车名,获取载客量,车辆租用量数,总金额等等 package class_car; public class Car { private String carName; private int carryCapacity; private int cargoCapacity; private int perMoney; private int perMoney; private int num; public int getNum() { return num; } public void setNum(int _num) { num += _num; } public String getCarName() { return carName; } public int getCarryCapacity() { return carryCapacity; } public int getCargoCapacity() { return cargoCapacity; } public int getPerMoney() { return perMoney; } } 子类方法继承,私有对象名字和每辆车的载客数载人数每辆车都用构造函数(构造方法)初始化,其余的不用初始化 例如 package class_car; public class AodiA4 extends Car { public AodiA4(){ carName = "奥迪"; carryCapacity = 4; perMoney = 500; cargoCapacity = 0; num = 0; moneyAll = 0; } private String carName; private int carryCapacity, perMoney, cargoCapacity; private int num, moneyAll; }
剩下的 main 函数里 调用选择函数和打印总价函数
对象编号123456 可以用建立对象数组的方法分别为 012345 不用过说了吧
创建父类Traffic.java
package com.traffic; public class Traffic { String name = "Traffic"; private int passengerCapacity; //Encapsulate passengerCapacity, just for test :D public int getPC(){ return passengerCapacity; } public void setPC(int newPC){ passengerCapacity = newPC; } /** * @param a */ public void whoAmI(Traffic a){ System.out.println(name + " can load " + a.getPC() + "people."); } }
创建子类Car.java
package com.traffic; public class Car extends Traffic { String name = "Car"; public void whoAmI(Traffic a){ System.out.println(name + " can load " + a.getPC() + " people."); } }
创建子类Airplane.java
package com.traffic; public class Airplane extends Traffic { String name = "Airplane"; public void whoAmI(Traffic a){ System.out.println(name + " can load " + a.getPC() + " people."); } }
创建子类Boat.java
package com.traffic; public class Boat extends Traffic { String name = "Boat"; public void whoAmI(Traffic a){ System.out.println(name + " can load " + a.getPC() + " people."); } }
创建程序人口Initial.java
package com.traffic; public class Initial { /** * @param args */ public static void main(String[] args) { // Create objects Traffic car = new Car(); Traffic airplane = new Airplane(); Traffic boat = new Boat(); //set passenger capacity car.setPC(8); airplane.setPC(400); boat.setPC(200); //call function whoAmI() car.whoAmI(car); airplane.whoAmI(airplane); boat.whoAmI(boat); } }
举报