1. 设计Person类, (1)包含姓名(String)、性别(String)和年龄(int)成员变量,以及相应的getter和setter方法。 (2) 至少实现一个构造方法 (3)重写toString方法,输出Person对象信息; 2. 以Person为父类,设计一个学生类Student, (1)增加学号(int)、班级(String)、英语成绩、数学成绩和java成绩 这几个成员变量,以相应的getter和setter方法。 (2) 至少实现一个构造方法 (3) 重写toString()方法,输出Student对象信息。
时间: 2023-05-25 11:05:03 浏览: 400
1. Person类代码:
```java
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 String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "姓名:" + name + ",性别:" + gender + ",年龄:" + age;
}
}
```
2. 学生类Student代码:
```java
public class Student extends Person {
private int studentNumber; // 学号
private String className; // 班级
private double englishScore; // 英语成绩
private double mathScore; // 数学成绩
private double javaScore; // Java成绩
public Student(String name, String gender, int age, int studentNumber, String className,
double englishScore, double mathScore, double javaScore) {
super(name, gender, age);
this.studentNumber = studentNumber;
this.className = className;
this.englishScore = englishScore;
this.mathScore = mathScore;
this.javaScore = javaScore;
}
public int getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(int studentNumber) {
this.studentNumber = studentNumber;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
public double getJavaScore() {
return javaScore;
}
public void setJavaScore(double javaScore) {
this.javaScore = javaScore;
}
@Override
public String toString() {
return super.toString() + ",学号:" + studentNumber + ",班级:" + className +
",英语成绩:" + englishScore + ",数学成绩:" + mathScore + ",Java成绩:" + javaScore;
}
}
```
阅读全文