2014-07-12 15 views
1

我嘗試更改coupe的值,但輸出不會更改。在NetBeans中,我將這兩個程序保存在同一個項目和包中。我沒有在這裏包含它,因爲它可能使代碼太長了,但我也寫了可以正常工作的accessor方法,所以我很困惑爲什麼mutator方法不起作用。Mutator方法不起作用,NetBeans

類代碼:

package auto; 

public class Auto 
{ 
    private String model; 
    private int milesDriven; 
    private double gallonsOfGas; 

    public Auto(String startModel, 
       int startMilesDriven, 
       double startGallonsOfGas) 
    { 
    model = startModel; 
    setMilesDriven(startMilesDriven); 
    setGallonsOfGas(startGallonsOfGas); 
    } 

    public void setModel(String newModel) 
    { 
     model = newModel; 
    } 

    public void setMilesDriven(int newMilesDriven) 
    { 
     if (newMilesDriven >= 0) 
      milesDriven = newMilesDriven; 
     else 
     { 
      System.err.println("Miles driven cannot be negative."); 
      System.err.println("Value not changed."); 
     } 
    } 

    public void setGallonsOfGas(double newGallonsOfGas) 
    { 
     if (newGallonsOfGas >= 0.0) 
      gallonsOfGas = newGallonsOfGas; 
     else 
     { 
      System.err.println("Gallons of gas cannot be negative."); 
      System.err.println("Value not changed."); 
     } 
    } 
} 

客戶端類代碼:

package auto; 

import java.text.DecimalFormat; 

public class AutoClient 
{ 
    public static void main(String [] args) 
    { 
     DecimalFormat milesPattern = new DecimalFormat("#,###"); 

     Auto coupe = new Auto("Corvette", 300000, 0.0); 

     String coupeModel = coupe.getModel(); 
     int coupeMiles = coupe.getMilesDriven(); 
     double coupeGallons = coupe.getGallonsOfGas(); 

     System.out.println("coupe:" 
          + "\nmodel: " + coupeModel 
          + "\nmiles: " + milesPattern.format(coupeMiles) 
          + "\ngallons: " + coupeGallons);  

     coupe.setModel("Viper"); 
     coupe.setMilesDriven(10000); 
     coupe.setGallonsOfGas(50.0); 

     System.out.println("coupe:" 
          + "\nmodel: " + coupeModel 
          + "\nmiles: " + milesPattern.format(coupeMiles) 
          + "\ngallons: " + coupeGallons); 
    } 
} 
+2

你能解釋爲什麼你認爲Netbeans與這個問題有關嗎? –

+0

最初,我認爲這是我保存文件的一個問題(例如,它們是否在同一個軟件包中)。我是IDE和編程新手。 – pez

+2

夠公平的。但是,如果這是問題所在,則會出現編譯錯誤,而不是運行的程序,但會給出錯誤答案。 –

回答

3

鑑於當前的代碼,更改了值

coupe.setModel("Viper"); 
coupe.setMilesDriven(10000); 
coupe.setGallonsOfGas(50.0); 

您需要再次讓他們

coupeModel = coupe.getModel(); 
coupeMiles = coupe.getMilesDriven(); 
coupeGallons = coupe.getGallonsOfGas(); 

之前,你可以調用

System.out.println("coupe:" 
         + "\nmodel: " + coupeModel 
         + "\nmiles: " + milesPattern.format(coupeMiles) 
         + "\ngallons: " + coupeGallons); 

我建議你更新Auto並添加toString()

@Override 
public String toString() { 
    return "coupe:" 
      + "\nmodel: " + coupeModel 
      + "\nmiles: " + milesPattern.format(coupeMiles) 
      + "\ngallons: " + coupeGallons; 
} 

然後你可以替換(在兩個地方)

System.out.println("coupe:" 
      + "\nmodel: " + coupeModel 
      + "\nmiles: " + milesPattern.format(coupeMiles) 
      + "\ngallons: " + coupeGallons); 

隨着

System.out.println(coupe); // <-- Java will call toString() for you 
+0

謝謝!我怎麼能修改'toString'來根據我調用它的位置/方式來返回相同的格式,但對於'Auto'類的各種實例,比如'sedan'和'suv'? – pez

+1

爲什麼不在'toString()'中添加一個'String vehicleType =「coupe」;'field,並將「coupe:」改爲'vehicleType +「:」''?然後你可以有一個'setVehicleType(String)'增變器。 –