package homeworkp146;
public class SuperclassStudent {
protected double StudentNumber;
protected String Name;
protected double English;
protected double Math;
protected double Computer;
protected double Sum;
protected double SverageScore;
//用set方法加this关键字将形式参数名声明为示例变量名
public void setStudentNumber(double StudentNumber) {
this.StudentNumber=StudentNumber;
}
public void setName(String Name) {
this.Name=Name;
}
public void setEnglish(double English) {
this.English=English;
}
public void setMath(double Math) {
this.Math=Math;
}
public void setComputer(double Computer) {
this.Computer=Computer;
}
public void setSum(double Sum){
this.Sum=Sum;
}
//get方法用于取得属性变量的值
public double getStudentNumber() {
return StudentNumber;
}
public String getName() {
return Name;
}
public double getEnglish() {
return English;
}
public double getMath() {
return Math;
}
public double getComputer() {
return Computer;
}
public double getSum() {
Sum=English+Math+Computer;
return Sum;
}
//声明testScore方法
public double testScore() {//在前面加(double)进行强制转换类型
SverageScore=(double)((English+Math+Computer)/3);//计算平均值
return SverageScore;
}
//定义方法
SuperclassStudent(){
}
public SuperclassStudent(int SverageScore,String Name,int English,int Math,int Computer) {
this.SverageScore=SverageScore;
this.Name=Name;
this.English=English;
this.Math=Math;
this.Computer=Computer;
}
//定义compare方法
public void compare(double a,double b,double c) {
if(a>b) {
if(a>c) {
System.out.println("三个数里"+a+"最大");
}else {
System.out.println("三个数里"+c+"最大");
}
}else if(b>c) {
System.out.println("三个数里"+b+"最大");
}else {
System.out.println("三个数里"+c+"最大");
}
}
}
学生父类
package homeworkp146;
public class SubclassXW extends SuperclassStudent{
String XWduty;//声明责任属性为字符串类型
SubclassXW(String XWduty,
double SverageScore,String Name,
double English,double Math,double Computer ){
// super(SverageScore, Name, English, Math, Computer);
super.SverageScore=SverageScore;
super.Name=Name;
super.English=English;
super.Math=Math;
super.Computer=Computer;
this.XWduty=XWduty;
}
public String XWduty() {
return XWduty;
}
//覆盖testScore
public double testScore() {
SverageScore=(double)((English+Math+Computer)/3+3);
return SverageScore;
}
}
学习委员子类
package homeworkp146;
public class SubclassBZ extends SuperclassStudent{
protected String BZduty;
public SubclassBZ(String BZduty,double SverageScore,
String Name,double English,double Math,double Computer ){
super.SverageScore=SverageScore;
super.Name=Name;
super.English=English;
super.Math=Math;
super.Computer=Computer;
this.BZduty=BZduty;
}
//
public String getBZduty() {
return BZduty;
}
//覆盖testScore方法
public double testScore() {
SverageScore=(double)((English+Math+Computer)/3+5);
return SverageScore;
}
}
班长子类
package homeworkp146;
public class TestPrinClass {
public static void main(String[] args) {
SuperclassStudent s1=new SuperclassStudent(38,"小贺",76,80,85);
s1.getSum();
System.out.println("学号:"+s1.SverageScore+" 姓名:"+s1.getName()
+" 英语:"+s1.English+" 数学:"+s1.Math+" 计算机成绩:"+s1.Computer
+" 总成绩"+s1.Sum);
System.out.println("average="+s1.testScore());
SubclassXW s2=new SubclassXW("学习委员",36,"老贺",76,80,85);
s2.getSum();
System.out.println("职务:"+s2.XWduty+" 学号:"+s2.SverageScore+
" 姓名:"+s2.getName()+" 英语:"+s2.English+" 数学:"+
s2.Math+" 计算机成绩:"+s2.Computer+" 总成绩"+s2.Sum);
System.out.println("average="+s2.testScore());
SubclassBZ s3=new SubclassBZ("班长",39,"老袁",76,89,85);
s3.getSum();
System.out.println("职务:"+s3.BZduty+" 学号:"+s3.SverageScore+
" 姓名:"+s3.getName()+" 英语:"+s3.English+" 数学:"+
s3.Math+" 计算机成绩:"+s3.Computer+" 总成绩"+s3.Sum);
System.out.println("平均分="+s3.testScore());
s1.compare(s1.Sum,s2.getSum(), s3.getSum());
}
}
测试输出主类
作业题目:
完成下面父类及子类的声明
(1) 声明Student类属性包括学号、姓名、英语成绩、数学成绩、计算机成绩和总成绩。方法包括构造方法、get方法、 set方法、toString方法、equals方法、compare方法(比较两个学生的总成绩,结果分为大于、小于、等于),sum方法(计算总成绩)和testScore方法(计算评测成绩)。
注:评测成绩可以取三门课成绩的平均分,另外任何一门课的成绩的改变都需要对总成绩进行重新计算,因此,在每一个set方法中应调用sum方法计算总成绩。
(2)声明StudentXW(学习委员)类为Student类的子类。
在StudentXW类中增加责任属性,并重写testScore方法(评测成绩=三门课平均分+3)
(3)声明StudentBZ类为Student类的子类
在StudentBZ类中增加责任属性,并重写testScore方法(评测成绩=三门课平均分+5)
4)声明测试类,生成若干个Student类、StudentXW类及StudentBZ类对象,并分别计算它们的评测成绩(建议采用:写一个测试函数,该函数以父类student数组作为参数) 。
参考资料来自