java定义一个父类 Person 及其两个子类 Student 和 Teacher:(1)为每个类提供适当的成员变量和方法(2)为每个类重写 toString 方法 (3)在 main 方法中定义一个 Person| 数组保存若干个 Student 和 Teacher 对象作为通讯录(每个数组元素变量指向一个具体的 Student 或 Teacher 对象)(4)遍历打印出数组的每一个元素 (5)程序可通过name、email、address 等信息进行查询(6)编写主类验证上述要求
时间: 2024-03-18 07:41:44 浏览: 80
Person类和它的两个子类Student、Employee
5星 · 资源好评率100%
好的,我会为您解答Java相关问题。
首先,我们需要定义一个父类Person,以及两个子类Student和Teacher,代码如下:
```java
public class Person {
private String name;
private int age;
private String email;
private String address;
public Person(String name, int age, String email, String address) {
this.name = name;
this.age = age;
this.email = email;
this.address = address;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getEmail() {
return email;
}
public String getAddress() {
return address;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setEmail(String email) {
this.email = email;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", email='" + email + '\'' +
", address='" + address + '\'' +
'}';
}
}
public class Student extends Person {
private String school;
public Student(String name, int age, String email, String address, String school) {
super(name, age, email, address);
this.school = school;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
@Override
public String toString() {
return "Student{" +
"name='" + getName() + '\'' +
", age=" + getAge() +
", email='" + getEmail() + '\'' +
", address='" + getAddress() + '\'' +
", school='" + school + '\'' +
'}';
}
}
public class Teacher extends Person {
private String subject;
public Teacher(String name, int age, String email, String address, String subject) {
super(name, age, email, address);
this.subject = subject;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + getName() + '\'' +
", age=" + getAge() +
", email='" + getEmail() + '\'' +
", address='" + getAddress() + '\'' +
", subject='" + subject + '\'' +
'}';
}
}
```
接下来,在main方法中定义一个Person数组来保存若干个Student和Teacher对象,代码如下:
```java
public static void main(String[] args) {
Person[] contacts = new Person[5];
contacts[0] = new Student("Tom", 18, "tom@example.com", "Beijing", "Tsinghua University");
contacts[1] = new Teacher("Jack", 35, "jack@example.com", "Shanghai", "Math");
contacts[2] = new Teacher("Linda", 30, "linda@example.com", "Guangzhou", "English");
contacts[3] = new Student("John", 20, "john@example.com", "Nanjing", "Southeast University");
contacts[4] = new Student("Lucy", 19, "lucy@example.com", "Hangzhou", "Zhejiang University");
// 遍历打印出数组的每一个元素
for (Person contact : contacts) {
System.out.println(contact.toString());
}
// 程序可通过name、email、address等信息进行查询
String query = "tom@example.com";
for (Person contact : contacts) {
if (contact.getEmail().equals(query)) {
System.out.println(contact.toString());
}
}
}
```
以上代码实现了所有的要求,您可以在本地编译运行验证。
阅读全文