public class PersonCar extends Car implements IZaiRen { ?? public int capacityPerson; ? ? ? ?? public PersonCar(int number,String carName,int price,int capacityPerson) { ?? ??? super(number,carName,price); ?? ??? this.capacityPerson=capacityPerson; ?? ??? this.setCarType ("PersonCar"); ?? } ? ? ?? public void zaiRen() { ????? System.out.print("? 載人:"+capacityPerson+"人 "); ?? } ? ? ?? public void print() { ?? ??? System.out.print(getNumber()+".? "+getCarName()+"? "+getPrice()+"元/天 "); ?? ??? this.zaiRen(); ?? }
}
public class StuffCar extends Car implements IZaiHuo { ?? public int capacityStuff; ? ? ?? public StuffCar(int number,String carName,int price,int capacityStuff) { ?? ??? super(number,carName,price); ?? ??? this.capacityStuff=capacityStuff; ?? ??? this.setCarType ("StuffCar"); ?? } ? ? ?? public void zaiHuo() { ?? ??? System.out.print("? 載貨:"+capacityStuff+"噸 "); ?? } ? ? ?? public void print() { ?? ??? System.out.print(getNumber()+".? "+getCarName()+"? "+getPrice()+"元/天 "); ?? ??? this.zaiHuo();; ?? }
}
public class PersonStuffCar extends Car implements IDouZai { ?? public int capacityPerson; ?? public int capacityStuff; ? ? ?? public PersonStuffCar(int number,String carName,int price,int capacityPerson,int capacityStuff) { ?? ??? super(number,carName,price); ?? ??? this.capacityPerson=capacityPerson; ?? ??? this.capacityStuff=capacityStuff; ?? ??? this.setCarType ("PersonStuffCar");
2018-09-24
我的代碼 供你參考 封裝性的話 get和set方法怕麻煩沒有用 你可以自己改改看 下面也有慕友的代碼用到了 ~
2018-09-24
根據你的要求進行了修改 封裝 多態 接口都用到了哦
public interface IZaiRen {
?? void zaiRen();
}
public interface IZaiHuo {
?? void zaiHuo();
}
public interface IDouZai extends IZaiHuo, IZaiRen {
}
public abstract class Car {
?? ?private int number;
??? private String carName;
??? private int price;
?? ?private String carType ;
?? ?
?? ?public Car(int number,String carName,int price) {
?? ??? ?this.number=number;
?? ??? ?this.carName=carName;
?? ??? ?this.price=price;
?? ?}
?? ?
??? public int getNumber() {
?? ??? ?return number;
?? ?}
?? ?public void setNumber(int number) {
?? ??? ?this.number = number;
?? ?}
?? ?public String getCarName() {
?? ??? ?return carName;
?? ?}
?? ?public void setCarName(String carName) {
?? ??? ?this.carName = carName;
?? ?}
?? ?public int getPrice() {
?? ??? ?return price;
?? ?}
?? ?public void setPrice(int price) {
?? ??? ?this.price = price;
?? ?}
?? ?public String getCarType() {
?? ??? ?return carType;
?? ?}
?? ?public void setCarType(String carType) {
?? ??? ?this.carType = carType;
?? ?}
?? ?
??? public abstract void print();
}
public class PersonCar extends Car implements IZaiRen {
?? public int capacityPerson;
?
? ?
?? public PersonCar(int number,String carName,int price,int capacityPerson) {
?? ??? super(number,carName,price);
?? ??? this.capacityPerson=capacityPerson;
?? ??? this.setCarType ("PersonCar");
?? }
? ?
?? public void zaiRen() {
????? System.out.print("? 載人:"+capacityPerson+"人 ");
?? }
? ?
?? public void print() {
?? ??? System.out.print(getNumber()+".? "+getCarName()+"? "+getPrice()+"元/天 ");
?? ??? this.zaiRen();
?? }
}
public class StuffCar extends Car implements IZaiHuo {
?? public int capacityStuff;
? ?
?? public StuffCar(int number,String carName,int price,int capacityStuff) {
?? ??? super(number,carName,price);
?? ??? this.capacityStuff=capacityStuff;
?? ??? this.setCarType ("StuffCar");
?? }
? ?
?? public void zaiHuo() {
?? ??? System.out.print("? 載貨:"+capacityStuff+"噸 ");
?? }
? ?
?? public void print() {
?? ??? System.out.print(getNumber()+".? "+getCarName()+"? "+getPrice()+"元/天 ");
?? ??? this.zaiHuo();;
?? }
}
public class PersonStuffCar extends Car implements IDouZai {
?? public int capacityPerson;
?? public int capacityStuff;
? ?
?? public PersonStuffCar(int number,String carName,int price,int capacityPerson,int capacityStuff) {
?? ??? super(number,carName,price);
?? ??? this.capacityPerson=capacityPerson;
?? ??? this.capacityStuff=capacityStuff;
?? ??? this.setCarType ("PersonStuffCar");
?? }
? ?
?? public void zaiRen() {
?? ??? System.out.print("? 載人:"+capacityPerson+"人 ");
?? }
? ?
?? public void zaiHuo() {
?? ??? System.out.print("? 載貨:"+capacityStuff+"噸 ");
?? }
? ?
?? public void print() {
?? ??? System.out.print(getNumber()+".? "+getCarName()+"? "+getPrice()+"元/天 ");
?? ??? this.zaiRen();
?? ??? this.zaiHuo();
?? }
}
import java.util.Scanner;
public class Test {
?? ?public static void main(String[] args) {
?? ??? ?System.out.println("歡迎使用答答租車系統:");
?? ??? ?System.out.println("您是否要租車:1是 0否");
?? ??? ?Scanner scanner = new Scanner(System.in);
?? ??? ?int sellect = scanner.nextInt();
?? ??? ?if (sellect==0)
?? ??? ??? ?return;
?? ??? ?
?? ??? ?int totalPersonC = 0;
?? ??? ?int totalStuffC = 0;
?? ??? ?int totalPrice =? 0;
?? ??? ?int pCarNum=0;
?? ??? ?int sCarNum=0;
?? ??? ?
?? ??? ?System.out.println("您可租車的類型及其價目表:");
?? ??? ?System.out.println("序號? 汽車名稱????? 租金????????? 容量");
?? ??? ?Car cars[] =new Car[6];
?? ??? ?String pcars[]=new String[6];
?? ??? ?String scars[]=new String[6];
?? ??? ?
?? ??? ?cars[0]=new PersonCar(1,"奧迪A4",500,4);
?? ??? ?cars[1]=new PersonCar(2,"馬自達6",400,4);
?? ??? ?cars[2]=new PersonStuffCar(3,"皮卡雪??? ",450,4,2);
?? ??? ?cars[3]=new PersonCar(4,"金龍???? ",800,20);
?? ??? ?cars[4]=new StuffCar(5,"松花江?? ",400,4);
?? ??? ?cars[5]=new StuffCar(6,"依維柯?? ",1000,20);
?? ??? ?for(Car a : cars) {
?? ??? ??? ?a.print();
?? ??? ??? ?System.out.println("");
?? ??? ??? ?//System.out.print(a instanceof PersonCar);
?? ??? ??? ?//System.out.print(a instanceof StuffCar);
?? ??? ??? ?//System.out.print(a instanceof PersonStuffCar);
?? ??? ?}
?? ??? ?System.out.println("請輸入您要租汽車的數量");
?? ??? ?int num=scanner.nextInt();
?? ??? ?for (int i=1;i<=num;i++) {
?? ??? ??? ?System.out.println("請輸入第"+i+"量車的序號");
?? ??? ??? ?int n = scanner.nextInt();
?? ??? ??? ?totalPrice+=cars[n-1].getPrice();
?? ??? ??? ?
?? ??? ??? ?switch (cars[n-1].getCarType()) {
?? ??? ??? ?case "PersonCar":pCarNum+=1;
?? ??? ??? ??? ?pcars[pCarNum]=cars[n-1].getCarName();
?? ??? ??? ??? ?totalPersonC+=((PersonCar)cars[n-1]).capacityPerson;
?? ??? ??? ??? ?break;
?? ??? ??? ?case "StuffCar":sCarNum+=1;
?? ??? ??? ??? ?scars[sCarNum]=cars[n-1].getCarName();
?? ??? ??? ??? ?totalStuffC+=((StuffCar)cars[n-1]).capacityStuff;
?? ??? ??? ??? ?break;
?? ??? ??? ?case "PersonStuffCar":
?? ??? ??? ??? ?pCarNum+=1;
?? ??? ??? ??? ?pcars[pCarNum]=cars[n-1].getCarName();
?? ??? ??? ??? ?totalPersonC+=((PersonStuffCar)cars[n-1]).capacityPerson;
?? ??? ??? ??? ?sCarNum+=1;
?? ??? ??? ??? ?scars[sCarNum]=cars[n-1].getCarName();
?? ??? ??? ??? ?totalStuffC+=((PersonStuffCar)cars[n-1]).capacityStuff;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?System.out.println("請輸入租車天數:");
?? ??? ?totalPrice*=scanner.nextInt();
?? ??? ?System.out.println("您的賬單:");
?? ??? ?System.out.println("***可載人的車有:");
?? ??? ?for (String s:pcars) {
?? ??? ??? ?if(s!=null)
?? ??? ??? ??? ?System.out.print(s+"? ");
?? ??? ?}
?? ??? ?System.out.println("共載人:"+totalPersonC+"人");
?? ??? ?
?? ??? ?System.out.println("***可載貨的車有:");
?? ??? ?for (String s:scars) {
?? ??? ??? ?if(s!=null)
?? ??? ??? ??? ?System.out.print(s+"? ");
?? ??? ?}
?? ??? ?System.out.println("共載貨:"+totalStuffC+"噸");
?? ??? ?
?? ??? ?System.out.println("***租車總價格:"+totalPrice+"元");
?? ??? ?
?? ?}
}
2018-08-12
用接口好像也不需要特意去定義常量還有必須實現的方法啊
2018-08-12
我也不會實現接口,我直接定義主類,定義參數進行封裝,然后在子類帶參構造函數中用主類的setter初始化參數就好了。
2018-08-09
我希望能夠更好地理解和應用接口,最主要的還是熟悉面向對象
2018-08-09
接口其實沒必要