创建和显示学生信息,具体要求如下
定义Person类,在该类中定义成员变量name(姓名)sex (性别)和 age (年龄),然后定义有参构造方法,初始成员变量,最后定义成员方法oString0返回姓名、性别和年龄信息。
定义 Student 类继承erson类,在该类中增加成员变量no(学号)、scoreEn(英语成绩)、scoreMath (数学成绩)和 screCh (语文成绩),然后增加成员方法aver、max和min分别返回三门功课的平均分、最育分和最低分,最后重写 toString方法返回姓名、性别、年龄、学号、平均分、最高分和最低信息。
定义 StudentTest 类在 main0方法中实现创建和显示学生信息
class Person {
String name;
String sex;
int age;
public Person(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String toString() {
return "姓名:" + name + ",性别:" + sex + ",年龄:" + age;
}
}
class Student extends Person {
String no;
int scoreEn;
int scoreMath;
int scoreCh;
public Student(String name, String sex, int age, String no, int scoreEn, int scoreMath, int scoreCh) {
super(name, sex, age);
this.no = no;
this.scoreEn = scoreEn;
this.scoreMath = scoreMath;
this.scoreCh = scoreCh;
}
public double aver() {
return (scoreEn + scoreMath + scoreCh) / 3.0;
}
public int max() {
int max = scoreEn > scoreMath ? scoreEn : scoreMath;
max = max > scoreCh ? max : scoreCh;
return max;
}
public int min() {
int min = scoreEn < scoreMath ? scoreEn : scoreMath;
min = min < scoreCh ? min : scoreCh;
return min;
}
public String toString() {
return super.toString() + ",学号:" + no + ",平均分:" + aver() + ",最高分:" + max() + ",最低分:" + min();
}
}
public class StudentTest {
public static void main(String[] args) {
Student stu = new Student("张三", "男", 18, "20210001", 80, 90, 70);
System.out.println(stu.toString());
}
}