簡體   English   中英

NetBeans的Mutator方法不起作用

[英]Mutator methods not working, NetBeans

我嘗試改變coupe的值,但輸出沒有變化。 在NetBeans中,我將這兩個程序保存在同一個項目和包中。 我沒有把它包含在這里,因為它可能使代碼太長了,但我也寫了一些工作正常的訪問器方法,所以我很困惑為什么mutator方法不起作用。

班級代碼:

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); 
    }
}

在更改值之后,根據您當前的代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM