如何防止我的setter方法中的参数被更改?

时间:2018-03-31 10:10:37

标签: java setter

例如我有这段代码:

public class Weather {
    private int temperature;

    //Set temperature to t.
    //If t goes below -15, temperature should become -15.
    //If t goes above 40, temperature should become 40.
    //If t is already between -15 and 40, it should not be changed.
    public void setTemperature(int t){
        if(t>= -15 && t<= 40){
            temperature = t;
        }
        else if(t< -15){
            temperature = -15;
        }
        else if(t> 40){
            temperature = 40;
        }
    }
}

我的问题是,如果说客户写了

Weather temp1 = new Weather();
temp1.setTemperature(-20);
temp1.setTemperature(10);

然后温度实际上会设置为10而不是-15,因为setTemperature函数被调用两次。如果温度已经设置为-15到40之间的值,我需要我的代码忽略setter函数的任何未来调用。我该怎么办?上面的代码就是我现在所有的......

5 个答案:

答案 0 :(得分:1)

尝试使用Integer而不是nullable状态:

private Integer temperature = null;

public void setTemperature(int t){
    if( temperature != null ) return;

    if(t>= -15 && t<= 40){
      temperature = t;
     }
     else if(t< -15){
      temperature = -15;
     }
     else if(t> 40){
      temperature = 40;
     }
    }
   }

答案 1 :(得分:0)

使用temperature初始化Integer.MAX_VALUE,如果其值小于setTemperature,则不要在public class TestMan { public class Weather { private int temperature = Integer.MAX_VALUE; //Set temperature to t. //If t goes below -15, temperature should become -15. //If t goes above 40, temperature should become 40. //If t is already between -15 and 40, it should not be changed. public void setTemperature(int t){ if (temperature < Integer.MAX_VALUE) return; if(t>= -15 && t<= 40){ temperature = t; } else if(t< -15){ temperature = -15; } else if(t> 40){ temperature = 40; } } } 内执行任何操作:

{{1}}

答案 2 :(得分:0)

根据您的要求:

  

如果温度已经设置为-15到40之间的值,我需要我的代码忽略setter函数的任何调用。

你可以使用一个标志来确定你之前是否已经调用了setter,如果是,那么检查以前的temperature是否在你的范围内,或者不在下面的代码中:

public class Weather {
private int temperature;
private boolean sette_called = false;
//Set temperature to t.
//If t goes below -15, temperature should become -15.
//If t goes above 40, temperature should become 40.
//If t is already between -15 and 40, it should not be changed.
public void setTemperature(int t){
    if (sette_called)
    {
        if (temperature >= -15 && temperature <= 40)
             return;
    }
    if(t>= -15 && t<= 40){
        temperature = t;
    }
    else if(t< -15){
        temperature = -15;
    }
    else if(t> 40){
        temperature = 40;
    }
    sette_called = true;
}
}

答案 3 :(得分:0)

尝试标记关键字,如果它是真的..我永远不会设置任何未来的温度        {

        private int temperature;
        bool flag = false;

        //Set temperature to t.
        //If t goes below -15, temperature should become -15.
        //If t goes above 40, temperature should become 40.
        //If t is already between -15 and 40, it should not be changed.
        public int setTemperature(int t)
        {
            if (flag == false)
            {
                if (t >= -15 && t <= 40)
                {
                    temperature = t;
                    flag = true;
                }
                else if (t < -15)
                {
                    temperature = -15;
                    flag = true;
                }
                else if (t > 40)
                {
                    temperature = 40;
                    flag = true;
                }
                return temperature;
            }
            else
            {
                return temperature;
            }
        }

答案 4 :(得分:-1)

使用Integer数据类型。设置temperature = null

private Integer temperature = null;

public void setTemperature(int t){
    if( temperature != null) {
        return;
    }

    if(t>= -15 && t<= 40){
      temperature = t;
    } else if(t< -15){
      temperature = -15;
    } else if(t> 40){
      temperature = 40;
    }
 }