设计一个抽象类名字为 Person,它的子类为Student和 Employee;person类有name以及toString方法;student有 status属性(freshman, sophomore, junior, or senior),定义status 为常量;Employee类 有office, salary;分别定义各个类的有参构造方法,Person(String name)、Student(String name,int status)、Employee(String name,String office,double salary),注意需要在子类构造方法第一句调用父类有参构造方法,为子类也实现toString方法。编写测试类定义一个数组存放每个非抽象类对象(Student和 Employee),然后利用多态调用toString方法打印各个对象的所有属性信息。(为了减少书写量,只需要为name属性完成get和set方法,其他属性不用写)
时间: 2024-04-08 09:35:12 浏览: 88
以下是根据您的要求设计的抽象类和子类的代码:
```java
// Person抽象类
public abstract class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public abstract String toString();
}
// Student子类
public class Student extends Person {
private static final int FRESHMAN = 1;
private static final int SOPHOMORE = 2;
private static final int JUNIOR = 3;
private static final int SENIOR = 4;
private int status;
public Student(String name, int status) {
super(name);
this.status = status;
}
public String getStatus() {
String statusStr;
switch (status) {
case FRESHMAN:
statusStr = "Freshman";
break;
case SOPHOMORE:
statusStr = "Sophomore";
break;
case JUNIOR:
statusStr = "Junior";
break;
case SENIOR:
statusStr = "Senior";
break;
default:
statusStr = "Unknown";
break;
}
return statusStr;
}
@Override
public String toString() {
return "Name: " + getName() + ", Status: " + getStatus();
}
}
// Employee子类
public class Employee extends Person {
private String office;
private double salary;
public Employee(String name, String office, double salary) {
super(name);
this.office = office;
this.salary = salary;
}
public String getOffice() {
return office;
}
public double getSalary() {
return salary;
}
@Override
public String toString() {
return "Name: " + getName() + ", Office: " + getOffice() + ", Salary: " + getSalary();
}
}
// 测试类
public class Test {
public static void main(String[] args) {
Person[] people = new Person[4];
people[0] = new Student("Alice", 3);
people[1] = new Student("Bob", 2);
people[2] = new Employee("Charlie", "HR", 50000);
people[3] = new Employee("Dave", "Finance", 60000);
for (Person person : people) {
System.out.println(person.toString());
}
}
}
```
这段代码定义了一个抽象类`Person`,它有一个`name`属性和一个`toString`方法。`Student`和`Employee`都是`Person`的子类,分别有额外的属性和构造方法,并且都实现了`toString`方法。在测试类中,使用多态性将`Student`和`Employee`对象存储在`Person`类型的数组中,并通过调用`toString`方法打印出各个对象的属性信息。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![java](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)