这个简单根据题目提示编写即可,主要是要学会体会Java的面向对象的思想,可以多敲代码多练习下。
代码如下:
参考链接:
package experiment;
public class Person {
public String name;// 姓名
public char sex; // 性别
public int age; // 年龄
public Person() { // 无参的构造方法
}
public Person(String s) { // 一个String参数的构造方法
this.name=s;
}
// 各成员变量的get和set方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
// toString方法,用于打印Person类对象的信息
public String toString() {
return "Person [姓名:" + name + ", 性别:" + sex + ", 年龄:" + age + "]";
}
}
Student.java:
package experiment;
public class Student extends Person{ // Student类,继承自Person类
public String no; // 学号
public double scoreOfEn; //英语成绩
public double scoreOfMath; // 数学成绩
public double scoreOfCh; // 语文成绩
public static void main(String[] args) {
// TODO Auto-generated method stub
// 创建Person类对象
Person p1 = new Person("张三");
p1.setSex('男'); // 设置对象的性别
p1.setAge(23); // 设置对象的年龄
System.out.println(p1); // 打印Person类对象p1的信息,此处会调用Person类的toString()方法来打印对象信息
// 创建Student类对象
Student s1 = new Student("李四",'男',23,"202103162689",60,70,80);
System.out.println(s1); // 打印Student类对象的信息,此处会调用Student类的toString()方法来打印对象信息
}
// 无参的构造
public Student() {
}
// 7个参数的构造方法,使用参数来各成员变量赋值
public Student(String name,char sex,int age,String no, double scoreOfEn, double scoreOfMath, double scoreOfCh) {
this.name = name;
this.sex= sex;
this.age = age;
this.no = no;
this.scoreOfEn = scoreOfEn;
this.scoreOfMath = scoreOfMath;
this.scoreOfCh = scoreOfCh;
}
//各成员变量的set和get方法
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public double getScoreOfEn() {
return scoreOfEn;
}
public void setScoreOfEn(double scoreOfEn) {
this.scoreOfEn = scoreOfEn;
}
public double getScoreOfMath() {
return scoreOfMath;
}
public void setScoreOfMath(double scoreOfMath) {
this.scoreOfMath = scoreOfMath;
}
public double getScoreOfCh() {
return scoreOfCh;
}
public void setScoreOfCh(double scoreOfCh) {
this.scoreOfCh = scoreOfCh;
}
// 计算三门功课平均成绩的方法
public double aver() {
double avg = (scoreOfCh+scoreOfMath+scoreOfEn)/3;
return avg;
}
// 计算三门功课最高分的方法
public double max() {
double maxScore=scoreOfEn;
if(maxScore<scoreOfMath) {
maxScore= scoreOfMath;
}
if(maxScore<scoreOfCh) {
maxScore = scoreOfCh;
}
return maxScore;
}
// 计算三门功课最低分的方法
public double min() {
double minScore=scoreOfEn;
if(minScore>scoreOfMath) {
minScore= scoreOfMath;
}
if(minScore>scoreOfCh) {
minScore = scoreOfCh;
}
return minScore;
}
@Override
// 覆盖父类同名的toString()方法,用于打印Student类对象的信息
public String toString() {
return "Student [ 学号:" + no +", 姓名:"+name+ ",性别:"+sex+", 平均分:" + aver() + ", 最高分:" + max() + ", 最低分:"
+ min() + "]";
}
}
你把,Person 类贴出来啊
比如这样
import java.util.Scanner;
class Person {
private String stuId;
private String name;
private double chineseScore;
private double mathScore;
private double englishScore;
public Person(String stuId, String name, double chineseScore, double mathScore, double englishScore) {
this.stuId = stuId;
this.name = name;
this.chineseScore = chineseScore;
this.mathScore = mathScore;
this.englishScore = englishScore;
}
public double calculateAverageScore() {
return (chineseScore + mathScore + englishScore) / 3;
}
public void displayInfo() {
System.out.println("学号:" + stuId);
System.out.println("姓名:" + name);
System.out.println("语文成绩:" + chineseScore);
System.out.println("数学成绩:" + mathScore);
System.out.println("英语成绩:" + englishScore);
}
}