2017-01-03 49 views
-1

我目前正試圖將2個子類重構爲超類。子類別稱爲TaxiShuttle,超類別稱爲Vehicle。我提出兩個重複的代碼,並提出在子類的構造函數調用的方法超級如下圖所示,但我不斷收到錯誤消息超類型構造函數被調用之前不能引用「X」錯誤消息

不能引用「標識」超構造函數被調用之前)

爲什麼?

這裏的Vehicle

public class Vehicle 
{ 
    // instance variables - replace the example below with your own 
    protected String ident; 
    // The Destination Of Taxi and Shuttle 
    protected String destination; 
    // The location of this shuttle. 
    protected String location; 

    /** 
    * Constructor for objects of class Vehicle 
    */ 
    public Vehicle(String id, String base) 
    { 
     // initialise instance variables 
     this.ident = id; 
     location = base; 
     destination = null; 
    } 

    /** 
    * Return the location of the shuttle. 
    * @return The location of the shuttle. 
    */ 
    public String getLocation() 
    { 
     return location; 
    } 

    /** 
    * Return the destination of the shuttle. 
    * @return The destination of the shuttle. 
    */ 
    public String getDestination() 
    { 
     return destination; 
    } 

    /** 
    * Return the ID of the shuttle. 
    * @return The ID of the shuttle. 
    */ 
    public String getIdent() 
    { 
     return ident; 
    } 

} 

這裏的Taxi

public class Taxi extends Vehicle 
{ 
    // Whether it is free or not. 
    private boolean free; 

    /** 
    * Constructor for objects of class Taxi. 
    * @param base The name of the company's base. 
    * @param id This taxi's unique id. 
    */ 
    public Taxi(String id, String base) 
    { 
     super(ident); 
     free = true; 
    } 

    /** 
    * Book this taxi to the given destination. 
    * The status of the taxi will no longer be free. 
    * @param destination The taxi's destination. 
    */ 
    public void book(String destination) 
    { 
     setDestination(destination); 
     free = false; 
    } 

    /** 
    * Return the status of this taxi. 
    * @return The status. 
    */ 
    public String getStatus() 
    { 
     return vehicle.ident + " at " + location + " headed for " + 
     destination; 
    } 


    /** 
    * Indicate that this taxi has arrived at its destination. 
    * As a result, it will be free. 
    */ 
    public void signalArrival() 
    { 
     location = destination; 
     destination = null; 
     free = true; 
    } 
} 

這裏的Shuttle

import java.util.ArrayList; 

/** 
* A shuttle. 
* Shuttles have a unique ID, a location and a list of destinations. 
* They operate a circular route. 
* 
* @author David J. Barnes 
* @version 2016.12.04 
*/ 
public class Shuttle extends Vehicle 
{ 

    private ArrayList<String> route; 
    // The destination number in route that the shuttle is 
    // currently headed for. 
    private int destinationNumber; 

    /** 
    * Constructor for objects of class Shuttle 
    * @param id This shuttle's unique id. 
    * @param route The route taken by this shuttle. 
    *    The first entry is the starting location. 
    */ 
    public Shuttle(ArrayList<String> route) 
    { 
     super(ident); 
     setRoute(route); 
    } 

    /** 
    * Return the status of this shuttle. 
    * @return The status. 
    */ 
    public String getStatus() 
    { 
     return ident + " at " + location + " headed for " + 
     destination; 
    } 


    /** 
    * Indicate that this shuttle has arrived at its next destination. 
    */ 
    public void signalArrival() 
    { 
     location = destination; 
     setNextDestination(); 
    } 

    /** 
    * Set the next destination of the shuttle. 
    */ 
    private void setNextDestination() 
    { 
     destinationNumber++; 
     if(destinationNumber >= route.size()) { 
      // End of the circular route. 
      // Start from the beginning again. 
      destinationNumber = 0; 
     } 
     setDestination(route.get(destinationNumber)); 
    } 

    /** 
    * Set the route for this shuttle. 
    * @param route The circular list of destinations. 
    */ 
    private void setRoute(ArrayList<String> route) 
    { 
     if(route.size() < 2) { 
      throw new IllegalStateException("setRoute must have at least two destinations"); 
     } 
     // Make a copy of the list parameter. 
     this.route = new ArrayList<String>(); 
     this.route.addAll(route); 
     destinationNumber = 0; 
     location = route.get(destinationNumber); 
     setNextDestination(); 
    } 

} 
+1

'Vehicle'沒有'Public Vehicle(String id)'構造函數。另外,通過傳遞給'Vehicle'它自己的'ident'變量,它從未初始化,您期望什麼? – Berger

+0

可以請你發表一個例子,請我理解但是我失去了。 –

回答

0

Vehicle沒有一個public Vehicle(String id)構造,只有public Vehicle(String id, String base)構造函數。

Taxi的構造函數應調用正確的(現有)超級構造函數。

注意,我們將局部變量id傳遞給超級構造函數,傳遞給Vehicle自己的空ident變量將毫無意義。

public Taxi(String id, String base) 
{ 
    super(id, base); 
    free = true; 
} 

Shuttle構造缺少的id參數(從它的文檔),讓我們添加它,(從它的文檔再次)使用列表的第一項作爲base參數:

 /** 
    * Constructor for objects of class Shuttle 
    * @param id This shuttle's unique id. 
    * @param route The route taken by this shuttle. 
    *    The first entry is the starting location. 
    */ 
    public Shuttle(String id, ArrayList<String> route) 
    { 
     super(id,route.get(0)); 
     setRoute(route); 
    } 
+0

感謝隊友非常感謝:D –

相關問題