//学术类
public class Student {
private String name;
private int age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) throws SexException {
if (age > 0 && age <= 120) {
this.age = age;
} else {
throw new SexException("输入年龄有误");
}
}
public String getSex() {
return sex;
}
public void setSex(String sex) throws Exception {
if ("男".equals(sex) || "女".equals(sex)) {
this.sex = sex;
} else {
throw new NameEception("输入的性别不存在");
}
}
public void show(){
System.out.println("我叫"+this.name+",我今年"+this.age+"岁了,是一个"+this.sex+"孩");
}
}
//测试类
public class Teststudent {
public static void main(String[] args) throws Exception {
Student s = new Student();
s.setName("张三");
s.setSex("男");
s.setAge(20);
s.show();
}
}
09-22
578
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-10
110
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)