定义学生类Student,该类继承自第1题的Person类。Student类中增加3个private的属性(变量):学号number,String类型;语文成绩chinese,float类型;数学成绩math,float类型。类中定义2个构造方法:无参数的构造方法,默认初始化;有参数的构造方法,首先调用父类构造方法初始化继承来的变量,初始化学号、语文成绩和数学成绩。覆盖父类的showResult()方法,满足子类描述对象信息的要求。最后,类中增加main方法,分别使用2个构造方法创建对象,并调用其showResult方法将对象的信息显示命令行的结果,结果如下:
时间: 2023-11-09 08:05:31 浏览: 71
代码如下:
```java
class Student extends Person {
private String number;
private float chinese;
private float math;
public Student() {
super();
this.number = "";
this.chinese = 0;
this.math = 0;
}
public Student(String name, int age, String gender, String number, float chinese, float math) {
super(name, age, gender);
this.number = number;
this.chinese = chinese;
this.math = math;
}
@Override
public void showResult() {
System.out.println("姓名:" + getName());
System.out.println("年龄:" + getAge());
System.out.println("性别:" + getGender());
System.out.println("学号:" + number);
System.out.println("语文成绩:" + chinese);
System.out.println("数学成绩:" + math);
}
public static void main(String[] args) {
Student s1 = new Student();
s1.showResult();
Student s2 = new Student("张三", 18, "男", "20191111", 90.5f, 85.0f);
s2.showResult();
}
}
```
输出结果如下:
```
姓名:
年龄:0
性别:
学号:
语文成绩:0.0
数学成绩:0.0
姓名:张三
年龄:18
性别:男
学号:20191111
语文成绩:90.5
数学成绩:85.0
```
阅读全文