java - 增变器方法不起作用,NetBeans

标签 java mutators

我尝试更改 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

关于java - 增变器方法不起作用,NetBeans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24708752/

相关文章:

java - 具有 Spring 安全功能的 Spring Boot : Error creating bean with name 'securityFilterChainRegistration'

java - 在背景中开始和停止音乐

java - 是否可以使用 th-field 来检索 Thymeleaf 中列表的值?

c++ - 为什么成员函数调用 "ambiguous"?

Java - 与 GridBagLayout 的两个代码比较 - IllegalArgumentException

java - 在 Jboss 上创建函数期间出现 SQL 语法错误

使用 let/set 设置 "remember"值的方案函数

swift - 正确应用封装

php - LARAVEL Payload is invalid 错误但解密数据是正确的

javascript - 试图理解无法在 javascript 中改变字符串/数字的概念