public class Person {
private String name;
private int age;
private String sno;
public Person(String name, int age, String sno) {
this.name = name;
this.age = age;
this.sno = sno;
if (age <= 0 || age > 130) {
System.out.println("非法年龄");
return;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age <= 0 || age > 130) {
System.out.println("非法年龄");
return;
}
this.age = age;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public static void main(String[] args) {
Person p = new Person("camila", 20, "17111107");
System.out.println(p.getName());
System.out.println(p.age);
System.out.println(p.sno);
}
}
定义一个Person类(name,age,sno)进行数据封装,age的年龄范围设置为0到130,使用getAge返回年龄。
最新推荐文章于 2023-03-27 17:17:30 发布