是的getter/setter对此很有用.
例如:
public void setAge(int age){
if(age < 0){
throw new IllegalArgumentException("Invalid age : " + age);
//or if you don't want to throw an exception you can handle it otherways too
}
}
Run Code Online (Sandbox Code Playgroud)
您也可以使用Java-EE的 豆校验此
public class Person{
@Min(value = 0)
@Max(value = 99)
private Integer age;
//some other code
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
195 次 |
| 最近记录: |