1)设计一个Person类和Student类,Person类包含属性班级banji和姓名name;有构造方法Person(String banji,String name),有toString方法显示信息;Student类继承Person类,有语文,数学,英语三门课程成绩chinese,math,english。有构造方法,计算总分方法sum,可以显示各科成绩和总分。 (2)创建Student类对象,(“一班”,”黎明”,90,85,95),显示出相关信息。
时间: 2023-06-09 14:04:35 浏览: 154
好的,我很明白您的要求,我会为您回答问题。以下是代码示例:
```java
// Person类
public class Person {
private String banji;
private String name;
public Person(String banji, String name) {
this.banji = banji;
this.name = name;
}
public String toString() {
return "班级:" + banji + ",姓名:" + name;
}
}
// Student类
public class Student extends Person {
private int chinese;
private int math;
private int english;
public Student(String banji, String name, int chinese, int math, int english) {
super(banji, name);
this.chinese = chinese;
this.math = math;
this.english = english;
}
public int sum() {
return chinese + math + english;
}
public void display() {
System.out.println(super.toString() + ",语文成绩:" + chinese + ",数学成绩:" + math + ",英语成绩:" + english + ",总分:" + sum());
}
}
// 创建Student类对象
Student student = new Student("一班", "黎明", 90, 85, 95);
student.display();
```
输出结果为:
```
班级:一班,姓名:黎明,语文成绩:90,数学成绩:85,英语成绩:95,总分:270
```
阅读全文