简体   繁体   English

NetBeans的Mutator方法不起作用

[英]Mutator methods not working, NetBeans

I try to change the values of coupe but the output doesn't change. 我尝试改变coupe的值,但输出没有变化。 In NetBeans, I saved these two programs under the same project and package. 在NetBeans中,我将这两个程序保存在同一个项目和包中。 I did not include it here because it may have made the code too long, but I also wrote accessor methods which work fine, so I am confused as to why the mutator methods don't work. 我没有把它包含在这里,因为它可能使代码太长了,但我也写了一些工作正常的访问器方法,所以我很困惑为什么mutator方法不起作用。

Class code: 班级代码:

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

Client class code: 客户端类代码:

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

Given your current code, after you changed the values 在更改值之后,根据您当前的代码

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

You need to get them again 你需要再次获得它们

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

Before you can call 在你打电话之前

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

I suggest you update Auto and add a toString() 我建议你更新Auto并添加一个toString()

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

Then you can replace (in both places) 然后你可以替换(在两个地方)

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

With

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM