(1)编写一个人类person,其中包含姓名、性别和年龄的属性,包含构造方法以及显示姓名、性别和年龄的方法。(10分) (2)再编写一个学生类student,它继承person类,其中包含学号属性,包含构造方法以及显示学号的方法。(10分) (3)在student类里面重写equals方法,只要是学号和姓名一致就认为是同一个学生。(20分) (4)最后编写一个测试类,此类里面包含main()方法,在main()方法中定义两个学生s1和s2并给他们赋值,最后显示他们的学号、姓名、性别以及年龄,以及判断两个学生是否是同一个学生。(10分)
时间: 2023-05-31 12:18:27 浏览: 826
### 回答1:
(1)人类person的代码:
public class Person {
private String name;
private String gender;
private int age;
public Person(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public void displayInfo() {
System.out.println("姓名:" + name);
System.out.println("性别:" + gender);
System.out.println("年龄:" + age);
}
}
(2)学生类student的代码:
public class Student extends Person {
private String studentId;
public Student(String name, String gender, int age, String studentId) {
super(name, gender, age);
this.studentId = studentId;
}
public void displayStudentId() {
System.out.println("学号:" + studentId);
}
}
(3)重写equals方法的代码:
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj == this) {
return true;
}
if (!(obj instanceof Student)) {
return false;
}
Student s = (Student) obj;
return this.getName().equals(s.getName()) && this.getStudentId().equals(s.getStudentId());
}
(4)测试类的代码:
public class Test {
public static void main(String[] args) {
Student s1 = new Student("张三", "男", 20, "2018001");
Student s2 = new Student("李四", "女", 21, "2018002");
s1.displayInfo();
s1.displayStudentId();
s2.displayInfo();
s2.displayStudentId();
if (s1.equals(s2)) {
System.out.println("s1和s2是同一个学生");
} else {
System.out.println("s1和s2不是同一个学生");
}
}
}
### 回答2:
(1)人类person:
public class Person {
String name;//姓名
char sex;//性别
int age;//年龄
public Person(String name, char sex, int age) {//构造方法
this.name = name;
this.sex = sex;
this.age = age;
}
public void display() {//显示姓名、性别和年龄的方法
System.out.println("姓名:" + name);
System.out.println("性别:" + sex);
System.out.println("年龄:" + age);
}
}
(2)学生类student:
public class Student extends Person {
String student_id;//学号
public Student(String name, char sex, int age, String student_id) {//构造方法
super(name, sex, age);//调用父类构造方法
this.student_id = student_id;
}
public void displayStudentId() {//显示学号的方法
System.out.println("学号:" + student_id);
}
}
(3)重写equals方法:
public class Student extends Person {
String student_id;//学号
public Student(String name, char sex, int age, String student_id) {
super(name, sex, age);
this.student_id = student_id;
}
public void displayStudentId() {
System.out.println("学号:" + student_id);
}
public boolean equals(Object obj) {//重写equals方法
if(obj instanceof Student) {//判断是否为学生对象
Student student = (Student)obj;
if(student.name.equals(this.name) && student.student_id.equals(this.student_id)) {//判断姓名和学号是否一致
return true;
}
}
return false;
}
}
(4)测试类:
public class Test {
public static void main(String[] args) {
Student s1 = new Student("张三", '男', 18, "001");
Student s2 = new Student("李四", '女', 20, "002");
s1.display();
s1.displayStudentId();
System.out.println("--------------------");
s2.display();
s2.displayStudentId();
System.out.println("--------------------");
if(s1.equals(s2)) {//判断是否为同一个学生
System.out.println("s1和s2是同一个学生");
}else {
System.out.println("s1和s2不是同一个学生");
}
}
}
### 回答3:
1. 人类Person类的编写:
public class Person {
private String name; // 姓名
private String sex; // 性别
private int age; // 年龄
// 构造方法
public Person(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
// 显示姓名、性别和年龄的方法
public void displayInfo() {
System.out.println("姓名:" + name);
System.out.println("性别:" + sex);
System.out.println("年龄:" + age);
}
}
2. 学生Student类的编写:
public class Student extends Person {
private String studentId; // 学号
// 构造方法
public Student(String name, String sex, int age, String studentId) {
super(name, sex, age);
this.studentId = studentId;
}
// 显示学号的方法
public void displayStudentId() {
System.out.println("学号:" + studentId);
}
}
3. 重写Student类中的equals方法:
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
Student student = (Student)obj;
return (student.getName().equals(this.getName()) && student.getStudentId().equals(this.getStudentId()));
}
4. 测试类的编写:
public class Test {
public static void main(String[] args) {
// 定义两个学生s1和s2并赋值
Student s1 = new Student("小明", "男", 18, "10001");
Student s2 = new Student("小红", "女", 19, "10001");
// 显示s1和s2的信息
System.out.println("s1信息:");
s1.displayInfo();
s1.displayStudentId();
System.out.println("----------------------");
System.out.println("s2信息:");
s2.displayInfo();
s2.displayStudentId();
// 判断s1和s2是否是同一个学生
if (s1.equals(s2)) {
System.out.println("s1和s2是同一个学生。");
} else {
System.out.println("s1和s2不是同一个学生。");
}
}
}
运行结果:
s1信息:
姓名:小明
性别:男
年龄:18
学号:10001
----------------------
s2信息:
姓名:小红
性别:女
年龄:19
学号:10001
s1和s2是同一个学生。
阅读全文