使用e.getMessage()编写符合下列条件的一个Java程序: 定义一个"年龄异常类":AgeException,使其继承自Exception,并添加无参、String参数的构造方法; 定义一个"性别异常类":SexException,使其继承自Exception,并添加无参、String参数的构造方法; 定义一个Student类,属性:姓名、性别、年龄。 在性别的set方法中判断是否是男/女,如果不是则抛出:性别异常 在年龄的set方法中判断年龄是否是15–50之间,如果不是则抛出:年龄异常 编写测试类,创建一个Student对象,并在try{}中调用setXxx()方法为对象属性赋值,在catch()中打印年龄错误。 注意:需要使用e.getMessage()
时间: 2024-03-02 22:52:47 浏览: 16
下面是符合要求的Java程序:
AgeException.java
```java
public class AgeException extends Exception {
public AgeException() {
super();
}
public AgeException(String message) {
super(message);
}
}
```
SexException.java
```java
public class SexException extends Exception {
public SexException() {
super();
}
public SexException(String message) {
super(message);
}
}
```
Student.java
```java
public class Student {
private String name;
private String sex;
private int age;
public Student(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) throws SexException {
if (!sex.equals("男") && !sex.equals("女")) {
throw new SexException("性别异常");
}
this.sex = sex;
}
public void setAge(int age) throws AgeException {
if (age < 15 || age > 50) {
throw new AgeException("年龄异常");
}
this.age = age;
}
}
```
Test.java
```java
public class Test {
public static void main(String[] args) {
Student student = new Student("张三", "男", 14);
try {
student.setSex("女");
} catch (SexException e) {
System.out.println(e.getMessage());
}
try {
student.setAge(60);
} catch (AgeException e) {
System.out.println(e.getMessage());
}
}
}
```
在这个程序中,我们定义了两个异常类:AgeException和SexException,继承自Exception类,并添加了无参和带有String参数的构造方法。
我们还定义了一个Student类,其中包含了姓名、性别、年龄三个属性,以及相应的get和set方法。在setSex和setAge方法中,我们加入了异常处理,如果输入的性别或年龄不符合条件,则抛出相应的异常。
在Test类中,我们创建了一个Student对象,并对其性别和年龄进行了赋值。由于输入的性别和年龄都不符合条件,所以会抛出相应的异常,并在catch语句中使用e.getMessage()方法获取异常信息并输出。
相关推荐
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)