简体   繁体   中英

How can I prevent my parameter in my setter method from being changed?

For example I have this code:

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

My problem is that if say there is a Client who writes

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

Then temperature would actually be set to 10 instead of -15, because the setTemperature function is called twice. I need my code to ignore any future calls of the setter function if temperature is already set to a value between -15 and 40. How do I do this? The above code is all I've got at the moment..

Try to use Integer instead with nullable state:

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

Initialize temperature with Integer.MAX_VALUE and don't do anything inside setTemperature if its value is less than that:

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

according to your request for :

I need my code to ignore any future calls of the setter function if temperature is already set to a value between -15 and 40.

you can use a flag to determine if you're setter has been called before and if yes then check to see if previous temperature is in your range or not like the code bellow:

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

try to flag key word if it true.. i will never set any future temperature {

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

Use Integer datatype. Set 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;
    }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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