Tuesday, September 25, 2012

Java範例 : 物件導向觀念 : 繼承, 多載, 覆載

範例程式碼中包含以下觀念 :
繼承 : RacingCar 繼承自 Car.
多載 : setCar()
覆載 : showCar()
類別變數 : carsum
類別函式 : showCarSum()

程式碼
class Car
{
 protected int id;
 protected double gasoline;
 private double MAXGAS = 2500;
 private static int carsum = 0;

 public Car()
 {
  id = 0;
  gasoline = 0.0;
  carsum++;
  this.manufactCar();
 }

 public Car(int id)
 {
  this();
  this.setCar(id);
 }

 public Car(int id, double gasoline)
 {
  this();
  this.setCar(id, gasoline);
 }

 public Car(double gasoline, int id)
 {
  this();
  this.setCar(gasoline, id);
 }

 private void prepareParts()
 {
  System.out.println("備料");
 }

 private void manufactCar()
 {
  this.prepareParts();
  System.out.println("製造一輛車");
 }

 public int getID()
 {
  return this.id;
 }

 public double getGas()
 {
  return this.gasoline;
 }

 public boolean setCar(int id)
 {
  if( id <= 0 ){
   System.out.println("!!! 車號 (" + id + ") 不可小於或等於 0. !!!");
   return false;
  } else {
   this.id = id;
   System.out.println("設定車號 " + this.id);
   return true;
  }
 }

 public boolean setCar(double gasoline)
 {
  if( gasoline < 0 || gasoline > this.MAXGAS ){
   System.out.println("!!! 油量 (" + gasoline + ") 不可小於 0 或大於 " + this.MAXGAS + ". !!!");
   return false;
  } else {
   this.gasoline = gasoline;
   System.out.println("車號 " + this.id + " 設定油量 " + this.gasoline);
   return true;
  }
 }

 public boolean setCar(int id, double gasoline)
 {
  return this.setCar(id) && this.setCar(gasoline);
 }

 public boolean setCar(double gasoline, int id)
 {
  return this.setCar(id) && this.setCar(gasoline);
 }

 public void showCar()
 {
  if( this.id == 0 ){
   System.out.print("車號 尚未設定 ");
  } else {
   System.out.print("車號 " + this.id + " ");
  }

  if( this.gasoline <= 0 ){
   System.out.print("尚未設定油量\n");
  } else {
   System.out.print("油量 " + this.gasoline + "\n");
  }
 }

 public static void showCarSum()
 {
  System.out.println("###### 總共生產 " + carsum + " 輛車. ######");
 }
}

class RacingCar extends Car
{
 private int turbo = 1;
 private int speed;
 private static int carsum = 0;

 public RacingCar()
 {
  speed = 300;
  carsum++;
  System.out.println("生產一輛賽車");
 }

 public RacingCar(int id)
 {
  super(id);
  speed = 300;
  carsum++;
  System.out.println("生產一輛賽車(編號 " + this.id + " 預設速度 " + speed + ")");
 }

 public RacingCar(int id, double gasoline)
 {
  super(id, gasoline);
  speed = 300;
  carsum++;
  System.out.println("生產一輛賽車(編號 " + this.id + " 油量 " + this.gasoline + " 預設速度 " + speed + ")");
 }

 public RacingCar(double gasoline, int id)
 {
  super(gasoline, id);
  speed = 300;
  carsum++;
  System.out.println("生產一輛賽車(編號 " + this.id + " 油量 " + this.gasoline + " 預設速度 " + speed + ")");
 }

 public RacingCar(int id, double gasoline, int speed)
 {
  super(id, gasoline);
  this.setSpeed(speed);
  carsum++;
  System.out.println("生產一輛賽車(編號 " + this.id + " 油量 " + this.gasoline + " 速度 " + this.speed + ")");
 }

 public RacingCar(double gasoline, int id, int speed)
 {
  super(gasoline, id);
  this.setSpeed(speed);
  carsum++;
  System.out.println("生產一輛賽車(編號 " + this.id + " 油量 " + this.gasoline + " 速度 " + this.speed + ")");
 }

 public void setSpeed(int speed)
 {
  int oldspeed = this.speed;
  if( oldspeed == speed ){
   System.out.println("賽車速度維持 " + this.speed);
  } else {
   this.speed = speed;
   System.out.println("將賽車速度由 " + oldspeed + " 調整為 " + this.speed);
  }
 }

 public void setTurbo(int turbo)
 {
  int oldturbo = this.turbo;
  if( oldturbo == turbo ){
   System.out.println("渦輪加速級數維持 " + this.turbo);
  } else {
   this.turbo = turbo;
   System.out.println("將渦輪加速級數由 " + oldturbo + " 調整為 " + this.turbo);
  }
 }

 public void showCar()
 {
  System.out.print("賽車");
  super.showCar();
  System.out.print(" 渦輪加速級數 " + this.turbo + " ");

  if( this.speed <= 0 ){
   System.out.print("尚未設定速度\n");
  } else {
   System.out.print("速度 " + this.speed + "\n");
  }
 }

 public static void showCarSum()
 {
  System.out.println("###### 總共生產 " + carsum + " 輛賽車. ######");
 }
}

class C11P271
{
 public static void main(String[] args)
 {
  String querystr = "[[[查詢車輛資訊]]]";
  String setupstr = "[[[設定車輛資訊]]]";

  System.out.println("\n [[[ GM 汽車生產陣列 ]]]");
  Car[] GM = new Car[12];
  int BASEID = 6600;
  for(int i = 0; i < GM.length; i++){
   if( i < 6 ){
    GM[i] = new Car(i+BASEID, 1688.88888);
   } else if( i >= GM.length - 2 ){
    //GM[i] = new Car(i+6600, 0.0); //OK
    GM[i] = new Car(i+BASEID);
    //GM[i].setCar(i+6600);
   } else {
    GM[i] = new Car(i+BASEID, 1666.666);
   }
  }
  Car.showCarSum();

  System.out.println("\n [[[ GM ]]] " + setupstr);
  GM[2].setCar(1888.88);
  GM[0].setCar(1888.88);
  GM[8].setCar(1888.88);
  System.out.println("\n [[[ GM ]]] " + querystr);
  for(int i = 0; i < GM.length; i++){
   GM[i].showCar();
  }

  Car.showCarSum();

  System.out.println("\n\n打造第一台賽車");
  RacingCar rc1 = new RacingCar();
  rc1.setSpeed(600);
  rc1.setCar(666, 2500.0);
  rc1.showCar();
  Car.showCarSum();
  RacingCar.showCarSum();

  System.out.println("\n [[[ LOTUS 賽車生產陣列 ]]]");
  RacingCar[] LOTUS = new RacingCar[6];
  BASEID = 8800;
  for(int i = 0; i < LOTUS.length; i++){
   if( i/3 == 0 ){
    ////LOTUS[i] = new RacingCar();
    ////LOTUS[i].setCar(i+BASEID, 2000.0);
    //LOTUS[i] = new RacingCar(i+BASEID, 2000.0);
    //LOTUS[i].setSpeed(500);
    LOTUS[i] = new RacingCar(i+BASEID, 2000.0, 500);
   } else {
    ////LOTUS[i] = new RacingCar();
    ////LOTUS[i].setCar(i+BASEID, 2500.0);
    //LOTUS[i] = new RacingCar(2500.0, i+BASEID);
    //LOTUS[i].setSpeed(660);
    LOTUS[i] = new RacingCar(2500.0, i+BASEID, 600);
   }
   System.out.print("調整渦輪加速級數 : ");
   LOTUS[i].setTurbo(i%3*2+1+i/3);
  }

  Car.showCarSum();
  RacingCar.showCarSum();

  System.out.println("\n [[[ LOTUS ]]] " + querystr);
  for(int i = 0; i < LOTUS.length; i++){
   LOTUS[i].showCar();
  }

  Car.showCarSum();
  RacingCar.showCarSum();

  System.out.println("\n [[[ Car type RacingCar ]]] ");
  Car dcar;
  dcar = new RacingCar();
  dcar.setCar(777, 1800.0);
  //dcar.setSpeed(360);
  System.out.println("\n [[[ Car type RacingCar ]]] " + querystr);
  dcar.showCar();

  Car.showCarSum();
  RacingCar.showCarSum();

  System.out.println("\n [[[ Car type RacingCar ]]] ");
  Car dcar1;
  dcar1 = new RacingCar(778, 1900.0, 660);
  System.out.println("\n [[[ Car type RacingCar ]]] " + querystr);
  dcar1.showCar();

  Car.showCarSum();
  RacingCar.showCarSum();

  System.out.println("\n [[[ MIT 房車/賽車生產陣列 ]]]");
  Car[] MIT = new Car[6];
  BASEID = 3600;
  for(int i = 0; i < LOTUS.length; i++){
   if( i/3 == 0 ){
    MIT[i] = new Car(i+BASEID, 1200.0);
   } else {
    MIT[i] = new RacingCar(i+BASEID, 2200.0, 500);
    // Compile error : P.267 Car type var. can not access sub-class function
    //System.out.print("調整渦輪加速級數 : ");
    //MIT[i].setTurbo(i%3*2+1+i/3);
   }
  }

  Car.showCarSum();
  RacingCar.showCarSum();

  System.out.println("\n [[[ MIT ]]] " + querystr);
  for(int i = 0; i < MIT.length; i++){
   MIT[i].showCar();
  }

  Car.showCarSum();
  RacingCar.showCarSum();
 }
}


執行結果
 [[[ GM 汽車生產陣列 ]]]
備料
製造一輛車
設定車號 6600
車號 6600 設定油量 1688.88888
備料
製造一輛車
設定車號 6601
車號 6601 設定油量 1688.88888
備料
製造一輛車
設定車號 6602
車號 6602 設定油量 1688.88888
備料
製造一輛車
設定車號 6603
車號 6603 設定油量 1688.88888
備料
製造一輛車
設定車號 6604
車號 6604 設定油量 1688.88888
備料
製造一輛車
設定車號 6605
車號 6605 設定油量 1688.88888
備料
製造一輛車
設定車號 6606
車號 6606 設定油量 1666.666
備料
製造一輛車
設定車號 6607
車號 6607 設定油量 1666.666
備料
製造一輛車
設定車號 6608
車號 6608 設定油量 1666.666
備料
製造一輛車
設定車號 6609
車號 6609 設定油量 1666.666
備料
製造一輛車
設定車號 6610
備料
製造一輛車
設定車號 6611
###### 總共生產 12 輛車. ######
 [[[ GM ]]] [[[設定車輛資訊]]]
車號 6602 設定油量 1888.88
車號 6600 設定油量 1888.88
車號 6608 設定油量 1888.88

 [[[ GM ]]] [[[查詢車輛資訊]]]
車號 6600 油量 1888.88
車號 6601 油量 1688.88888
車號 6602 油量 1888.88
車號 6603 油量 1688.88888
車號 6604 油量 1688.88888
車號 6605 油量 1688.88888
車號 6606 油量 1666.666
車號 6607 油量 1666.666
車號 6608 油量 1888.88
車號 6609 油量 1666.666
車號 6610 尚未設定油量
車號 6611 尚未設定油量
###### 總共生產 12 輛車. ######


打造第一台賽車
備料
製造一輛車
生產一輛賽車
將賽車速度由 300 調整為 600
設定車號 666
車號 666 設定油量 2500.0
賽車車號 666 油量 2500.0
 渦輪加速級數 1 速度 600
###### 總共生產 13 輛車. ######
###### 總共生產 1 輛賽車. ######

 [[[ LOTUS 賽車生產陣列 ]]]
備料
製造一輛車
設定車號 8800
車號 8800 設定油量 2000.0
將賽車速度由 0 調整為 500
生產一輛賽車(編號 8800 油量 2000.0 速度 500)
調整渦輪加速級數 : 渦輪加速級數維持 1
備料
製造一輛車
設定車號 8801
車號 8801 設定油量 2000.0
將賽車速度由 0 調整為 500
生產一輛賽車(編號 8801 油量 2000.0 速度 500)
調整渦輪加速級數 : 將渦輪加速級數由 1 調整為 3
備料
製造一輛車
設定車號 8802
車號 8802 設定油量 2000.0
將賽車速度由 0 調整為 500
生產一輛賽車(編號 8802 油量 2000.0 速度 500)
調整渦輪加速級數 : 將渦輪加速級數由 1 調整為 5
備料
製造一輛車
設定車號 8803
車號 8803 設定油量 2500.0
將賽車速度由 0 調整為 600
生產一輛賽車(編號 8803 油量 2500.0 速度 600)
調整渦輪加速級數 : 將渦輪加速級數由 1 調整為 2
備料
製造一輛車
設定車號 8804
車號 8804 設定油量 2500.0
將賽車速度由 0 調整為 600
生產一輛賽車(編號 8804 油量 2500.0 速度 600)
調整渦輪加速級數 : 將渦輪加速級數由 1 調整為 4
備料
製造一輛車
設定車號 8805
車號 8805 設定油量 2500.0
將賽車速度由 0 調整為 600
生產一輛賽車(編號 8805 油量 2500.0 速度 600)
調整渦輪加速級數 : 將渦輪加速級數由 1 調整為 6
###### 總共生產 19 輛車. ######
###### 總共生產 7 輛賽車. ######

 [[[ LOTUS ]]] [[[查詢車輛資訊]]]
賽車車號 8800 油量 2000.0
 渦輪加速級數 1 速度 500
賽車車號 8801 油量 2000.0
 渦輪加速級數 3 速度 500
賽車車號 8802 油量 2000.0
 渦輪加速級數 5 速度 500
賽車車號 8803 油量 2500.0
 渦輪加速級數 2 速度 600
賽車車號 8804 油量 2500.0
 渦輪加速級數 4 速度 600
賽車車號 8805 油量 2500.0
 渦輪加速級數 6 速度 600
###### 總共生產 19 輛車. ######
###### 總共生產 7 輛賽車. ######

 [[[ Car type RacingCar ]]] 
備料
製造一輛車
生產一輛賽車
設定車號 777
車號 777 設定油量 1800.0

 [[[ Car type RacingCar ]]] [[[查詢車輛資訊]]]
賽車車號 777 油量 1800.0
 渦輪加速級數 1 速度 300
###### 總共生產 20 輛車. ######
###### 總共生產 8 輛賽車. ######

 [[[ Car type RacingCar ]]] 
備料
製造一輛車
設定車號 778
車號 778 設定油量 1900.0
將賽車速度由 0 調整為 660
生產一輛賽車(編號 778 油量 1900.0 速度 660)

 [[[ Car type RacingCar ]]] [[[查詢車輛資訊]]]
賽車車號 778 油量 1900.0
 渦輪加速級數 1 速度 660
###### 總共生產 21 輛車. ######
###### 總共生產 9 輛賽車. ######

 [[[ MIT 房車/賽車生產陣列 ]]]
備料
製造一輛車
設定車號 3600
車號 3600 設定油量 1200.0
備料
製造一輛車
設定車號 3601
車號 3601 設定油量 1200.0
備料
製造一輛車
設定車號 3602
車號 3602 設定油量 1200.0
備料
製造一輛車
設定車號 3603
車號 3603 設定油量 2200.0
將賽車速度由 0 調整為 500
生產一輛賽車(編號 3603 油量 2200.0 速度 500)
備料
製造一輛車
設定車號 3604
車號 3604 設定油量 2200.0
將賽車速度由 0 調整為 500
生產一輛賽車(編號 3604 油量 2200.0 速度 500)
備料
製造一輛車
設定車號 3605
車號 3605 設定油量 2200.0
將賽車速度由 0 調整為 500
生產一輛賽車(編號 3605 油量 2200.0 速度 500)
###### 總共生產 27 輛車. ######
###### 總共生產 12 輛賽車. ######

 [[[ MIT ]]] [[[查詢車輛資訊]]]
車號 3600 油量 1200.0
車號 3601 油量 1200.0
車號 3602 油量 1200.0
賽車車號 3603 油量 2200.0
 渦輪加速級數 1 速度 500
賽車車號 3604 油量 2200.0
 渦輪加速級數 1 速度 500
賽車車號 3605 油量 2200.0
 渦輪加速級數 1 速度 500
###### 總共生產 27 輛車. ######
###### 總共生產 12 輛賽車. ######

No comments: