第一种:在构造方法中添加
如下代码:
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
//对年龄进行非法拦截
if(age<0||age>150) {
System.out.println("非法年龄");
}else {
this.age = age;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name