请有JAVA知识的人帮帮忙,Person 类已写好,其他的不确定应该怎么写
2条回答 默认 最新
- a5156520 2023-04-09 22:47关注
这个简单根据题目提示编写即可,主要是要学会体会Java的面向对象的思想,可以多敲代码多练习下。
代码如下:
参考链接:https://www.cnblogs.com/guankelei/p/11224047.htmlhttps://blog.csdn.net/qq_42530693/article/details/120964888
Person.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() + "]"; } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录
悬赏问题
- ¥15 使用yolov5-7.0目标检测报错
- ¥15 对于这个问题的解释说明
- ¥200 询问:python实现大地主题正反算的程序设计,有偿
- ¥15 smptlib使用465端口发送邮件失败
- ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
- ¥15 对于squad数据集的基于bert模型的微调
- ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
- ¥20 steam下载游戏占用内存
- ¥15 CST保存项目时失败
- ¥20 java在应用程序里获取不到扬声器设备