2017-08-04 65 views
0

我複製了一個自行車示例並想將其更改爲汽車示例,但無法運行類PolyCar的擴展類Mountainbike。如果我評論Mountainbike,PolyCar會運行。誰能告訴我,爲什麼程序無法識別山地在Java中運行我的多態性代碼示例時遇到問題

這裏是用來運行我PolyCar程序

//import PolyCar.MountainBike; 
//import PolyCar.RoadBike; 

public class TestCars { 
     public static void main(String[] args){ 
     PolyCar bike01, bike02, bike03; 

     bike01 = new PolyCar(20, 10, 1); 
     bike02 = new MountainBike(20, 10, 5, "Dual"); 
//  bike03 = new RoadBike(40, 20, 8, 23); 

     bike01.printDescription(); 
     bike02.printDescription(); 
//  bike03.printDescription(); 
     } 
    } 

這裏TestCar程序是我PolyCar代碼

public class PolyCar { 

    // the PolyCar class has three fields 
    public int cadence; 
    public int gear; 
    public int speed; 

    // the PolyCar class has one constructor 
    public PolyCar(int startCadence, int startSpeed, int startGear) { 
     gear = startGear; 
     cadence = startCadence; 
     speed = startSpeed; 
    } 

    // the PolyCar class has four methods 
    public void setCadence(int newValue) { 
     cadence = newValue; 
    } 

    public void setGear(int newValue) { 
     gear = newValue; 
    } 

    public void applyBrake(int decrement) { 
     speed -= decrement; 
    } 

    public void speedUp(int increment) { 
     speed += increment; 
    } 
public void printDescription(){ 
    System.out.println("\nBike is " + "in gear " + this.gear 
     + " with a cadence of " + this.cadence + 
     " and travelling at a speed of " + this.speed + ". "); 
} 
public class MountainBike extends PolyCar { 
    private String suspension; 

    public MountainBike(
       int startCadence, 
       int startSpeed, 
       int startGear, 
       String suspensionType){ 
     super(startCadence, 
       startSpeed, 
       startGear); 
     this.setSuspension(suspensionType); 
    } 

    public String getSuspension(){ 
     return this.suspension; 
    } 

    public void setSuspension(String suspensionType) { 
     this.suspension = suspensionType; 
    } 

    public void printDescription() { 
     super.printDescription(); 
     System.out.println("The " + "MountainBike has a" + 
      getSuspension() + " suspension."); 
    } 
} 
public class RoadBike extends PolyCar{ 
    // In millimeters (mm) 
    private int tireWidth; 

    public RoadBike(int startCadence, 
        int startSpeed, 
        int startGear, 
        int newTireWidth){ 
     super(startCadence, 
       startSpeed, 
       startGear); 
     this.setTireWidth(newTireWidth); 
    } 

    public int getTireWidth(){ 
     return this.tireWidth; 
    } 

    public void setTireWidth(int newTireWidth){ 
     this.tireWidth = newTireWidth; 
    } 

    public void printDescription(){ 
     super.printDescription(); 
     System.out.println("The RoadBike" + " has " + getTireWidth() + 
      " MM tires."); 
    } 
} 

} 
+0

都應該被註釋掉的import語句? – Jite

+1

將每個類提取到它自己的類文件。 – JimmyB

回答

0

你有山地車和RoadCike在PolyCar類的範圍內。

要做的邏輯事情是將它們取出(在MountainBike定義之前放置一個大括號,並在RoadBike類之後移除最後一個)。或者讓MountainBike和RoadBike類靜態並初始化它們,如bike02 = new PolyCar.MountainBike(20,10,5,「Dual」);

第二個選項是在我看來不好的,因爲後面有把他們作爲嵌套類沒有邏輯,當你試圖完成繼承&多態性和嵌套代表着一種「組成」的關係。

+0

非常感謝您的幫助。讓我的程序運行 – Elmore

+0

我改變了支架的位置 – Elmore

+0

@Elmore不客氣。如果帖子是你期望的,不要忘記通過驗證答案來支持;) – Maaaatt

1

你有MountainBikeRoadBike封裝在PolyCar類;他們應該在單獨的類文件中。您的解決方案與在聲明其他兩個子類之前移動最後一個大括號以得出PolyCar類一樣簡單。

目前您有:

public class PolyCar 
{ 
    ... 
    public class MountainBike extends PolyCar{ 
     ... 
    } 

    public class RoadBike extends PolyCar{ 
     ... 
    } 
} 

更改爲:

class PolyCar { 
    ... 
} 

class RoadBike extends PolyCar{ 
    .... 
} 

class RoadBike extends PolyCar{ 
    ... 
} 

工作TutorialsPoint例如here