繁体   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