类的继承(java)

该实验旨在理解Java中的类继承机制,通过声明Student类及其子类StudentXW和StudentBZ,实现属性和方法的继承与重写。实验涉及计算学生总成绩、评测成绩,并比较不同学生间的成绩。StudentXW和StudentBZ类分别增加了责任属性并调整了评测成绩的计算方式。最后通过测试类创建对象并计算评测成绩,展示类继承的应用。
摘要由CSDN通过智能技术生成

(一)实验类型:设计性

(二)实验目的:

1. 理解Java 中的类继承机制,;

2. 掌握声明和使用继承类的方法;

3. 培养良好的Java编程习惯。

(三)实验内容:

1. 声明Student类。

属性包括学号(number)、姓名(name)、英语成绩(englishScore)、数学成绩(mathScore)、计算机成绩(computerScore)和总成绩(totalScore)。方法包括构造方法、get方法、set方法、compare方法(比较两个学生的总成绩,结果分大于、小于、等于)、sum方法(计算总成绩)和testScore方法(计算评测成绩)。

注:评测成绩可以取三门课成绩的平均分,另外任何一门课的成绩的改变都需要对总成绩进行重新计算。

  1. 声明StudentXW(学习委员)类为Student类的子类。在StudentXW类中增加责任(responsibility)属性,并重写testScore方法(计算评测成绩,评测成绩=三门课的平均分+3)。
  2. 声明StudentBZ(班长)类为Student类的子类。在StudentBZ类中增加责任(responsibility)属性,并重写testScore方法(计算评测成绩,评测成绩=三门课的平均分+5)
  3. 声明测试类,生成2个Student类、1个StudentXW类及1个StudentBZ类对象,并分别计算它们的评测成绩,以及互相进行总成绩的对比。

输入描述:按照先2个Student类(number,name,englishScore,mathScore,computerScore),1个StudentXW类(number,name,englishScore,mathScore,computerScore,responsibility),1个StudentBZ类(number,name,englishScore,mathScore,computerScore,responsibility)对象输入数据。

输入案例:

101 Lisi 70 70 70 empty

101 Zhaoliu 70 70 70 empty

102 Zhangsan 90 90 90 Receivehomework

103 Wangwu 100 100 100 Leadeveryone

输出案例:

Lisi's evaluation score is 70.0

Zhangsan's evaluation score is 93.0

Wangwu's evaluation score is 105.0

Zhaoliu's evaluation score is 70.0

Zhangsan's total score is higher than Lisi

Zhangsan's total score is lower than Wangwu

Lisi's total score is equal to Zhaoliu

实验过程

//最大的收获就在于static可以保留数据不删除
//因此如果想要一直保留的值还是方法,加入static系统就不会删除,你就可以调用上一次的值!

    //输出的类和方法的类分开放,只要不重复public class即可
 class Student{
        private int number;
        private String name;
        private double englishScore;
        private double mathScore;
        private double computerScore;
        private static double totalScore;
        //设置为静态变量,保留值的大小不回收
        //方便父子类继承数据
        public Student(){}//因为要多态,所以先创立一个空方法。
        //因为是多态,还是在原来的Student这个static属性中,所以不需要自己加入void
        //其他的就要加void!
        public  Student(int number,String name,double englishScore,double mathScore,double computerScore)
        //方法,用于接收上传的数据,然后使用this来存到对象中
        {
            this.number=number;
            this.name=name;
            this.englishScore=englishScore;
            this.mathScore=mathScore;
            this.computerScore=computerScore;
            
        }

//        public void setNumber(int number) {
//            this.number = number;
//        }
//
//        public int getNumber() {
//            return number;
//        }
//
//        public void setName(String name) {
//            this.name = name;
//        }
    //暂时没有必要
    //其实这些set和get就是前面Student方法的一个拆分的版本,方便调用,如果我没有四个数据,只有一个数据上传
    //和用到我就只变换一个量

        //独立开出很多个set和get,这样方便修改单个数据,就不需要
        //总是传一堆数据上去!
        
        public String getName(){//存到对象中后,就可以直接使用对象的值了,所以就可以直接return {
            return name;
        }

        void compare(Student s1,Student s2){
            if(s1.totalScore>s2.totalScore)
                System.out.println(s1.name+"'s total score is higher than "+s2.name);
            if(s1.totalScore<s2.totalScore)
                System.out.println(s1.name+"'s total score is lower than "+s2.name);
            if(s1.totalScore==s2.totalScore)
                System.out.println(s1.name+"'s total score is equal to "+s2.name);
        }
        void sum(){
            totalScore=englishScore+mathScore+computerScore;
        }
        public double testScore(){
        	
            return (totalScore/3);
        }
    }
    //父类就单独一个class,独立,子类放在外面
    class StudentXW extends Student//父类和子类需要分开来放,不要把子类叠在父类中
    {
        private String responsibility;
        public void setResponsibility(String responsibility)//上传的数据
        //上传数据的时候要public,不然看不见!如果已经在对象中那就不需要public了
        {
            this.responsibility = responsibility;
            //this调用子类方法
        }

        public String getResponsibility() {
            return responsibility;
        }

        //@Override
        public double testScore() {
            return super.testScore()+3;
            //super,调用父类的方法
        }
    }
    class StudentBZ extends  Student{
        private String responsibility;
        public void setResponsibility(String responsibility) {
            this.responsibility = responsibility;
        }

        public String getResponsibility() {
            return responsibility;
        }

        //@Override
        public double testScore() {
            return super.testScore()+5;
        }
    }
    //子类仅仅只是用了父类的方法,但是父类的值是不能继承的,必须自己添加进去。
    //但是只要把totalScore设置为静态static,系统就不会自动帮你清除回收空间
    //你的totalScore就会保留,这就是static的好处!
    
    
    public class Main {
        public static void main(String args[]) 
        {
        	//由于不知道要与谁比对,所以说插入法没有必要,直接输入数据即可
            Student s1=new Student(12,"Lisi",70,70,70);
            s1.sum();
            System.out.println(s1.getName()+"'s evaluation score is " +s1.testScore());
            //这个是在使用输出的时候求出的testScore
            //实际上一个类写了很多方法不会自动输出,只有你调用的时候他才会计算出来
            //不然一直都是默认初始值!
            
           //学习委员特判
            Student s2=new Student(13,"Zhangsan",90,90,90);
            s2.sum();//这个别漏了,父类的成绩都没算怎么算子类的
           Student m=new StudentXW();
            double mm=m.testScore();
            s2=new Student(13,"Zhangsan",mm,mm,mm);//可以写个setget来只改变一个参数
            //这样更加简洁
            s2.sum();
            System.out.println(s2.getName()+"'s evaluation score is " +s2.testScore());
           
            
            //班长特判
            Student s3=new Student(14,"Wangwu",100,100,100);
            s3.sum();
           m=new StudentBZ();
           mm=m.testScore();
            s3=new Student(13,"Zhangsan",mm,mm,mm);
            s3.sum();
            System.out.println(s3.getName()+"'s evaluation score is " +s3.testScore());
           
            
            Student s4=new Student(15,"Zhaoliu",70,70,70);
            s4.sum();
            System.out.println(s4.getName()+"'s evaluation score is " +s4.testScore());
            s4.compare(s2,s1);
            s4.compare(s2,s3);
            s4.compare(s1,s4);
        }
    }


下面是更完整的代码,不过输出可能有些是用不上的,但是可以参考set和get的使用

 

public class Main {
    public static void main(String[] args) {
        Student s1=new Student(12,"Lisi",70,70,70);
        s1.sum();
        System.out.println(s1.getName()+"'s evaluation score is " +s1.testScore());
        Student s2=new Student(13,"Zhangsan",93,93,93);
        s2.sum();
        System.out.println(s2.getName()+"'s evaluation score is " +s2.testScore());
        Student s3=new Student(14,"Wangwu",105,105,105);
        s3.sum();
        System.out.println(s3.getName()+"'s evaluation score is " +s3.testScore());
        Student s4=new Student(15,"Zhaoliu",70,70,70);
        s4.sum();
        System.out.println(s4.getName()+"'s evaluation score is " +s4.testScore());
        System.out.println();
        s4.compare(s2,s1);
        s4.compare(s2,s3);
        s4.compare(s1,s4);
    }
    static class Student{
        private int number;
        private String name;
        private double englishScore;
        private double mathScore;
        private double computerScore;
        private double totalScore;
        public Student(){}
        public Student(int number,String name,double englishScore,double mathScore,double computerScore){
            this.number=number;
            this.name=name;
            this.englishScore=englishScore;
            this.mathScore=mathScore;
            this.computerScore=computerScore;
        }

        public void setNumber(int number) {
            this.number = number;
        }

        public int getNumber() {
            return number;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setEnglishScore(double englishScore) {
            this.englishScore = englishScore;
        }

        public double getEnglishScore() {
            return englishScore;
        }

        public void setMathScore(double mathScore) {
            this.mathScore = mathScore;
        }

        public double getMathScore() {
            return mathScore;
        }

        public void setComputerScore(double computerScore) {
            this.computerScore = computerScore;
        }

        public double getComputerScore() {
            return computerScore;
        }

        void compare(Student s1,Student s2){
            if(s1.totalScore>s2.totalScore)
                System.out.println(s1.name+"'s total score is higher than "+s2.name);
            if(s1.totalScore<s2.totalScore)
                System.out.println(s1.name+"'s total score is lower than "+s2.name);
            if(s1.totalScore==s2.totalScore)
                System.out.println(s1.name+"'s total score is equal to "+s2.name);
        }
        void sum(){
            totalScore=englishScore+mathScore+computerScore;
        }
        public double testScore(){
            return(totalScore/3);
        }
    }
    class StudentXW extends Student{
        private String responsibility;
        public void setResponsibility(String responsibility) {
            this.responsibility = responsibility;
        }

        public String getResponsibility() {
            return responsibility;
        }

        @Override
        public double testScore() {
            return super.testScore()+3;
        }
    }
    class StudentBZ extends  Student{
        private String responsibility;
        public void setResponsibility(String responsibility) {
            this.responsibility = responsibility;
        }

        public String getResponsibility() {
            return responsibility;
        }

        @Override
        public double testScore() {
            return super.testScore()+5;
        }
    }
}

后面是一开始解题的一些笔记,用的是对象数组和构造函数的方法,但识别不出,可能是版本不匹配导致的,既然通用性不高那就不算是一个比较好的算法

 

import java.util.Scanner;

 class Student
{
	private String number;//学会用private封装
	private String name;
    private float englishScore;
    private float mathScore;
    private float computerScore;//成绩可能会出现小数!
	private float totalScore;
	
	public Student(){}//重载类,多态就要先创建一个空的方法
	
	public Student(String name,float englishScore,float mathScore,float computerScore)//多态
	{
	
		this.name=name;
		this.englishScore=englishScore;
		this.mathScore=mathScore;
		this.computerScore=computerScore;
		
	}//作用就是为了创建使用新的对象的时候可以上传数据!
	
	
	String getName()
	{
		return this.name;
	}
	
	
	
	float getScore()
	{
		totalScore=englishScore+mathScore+computerScore;
		return totalScore/3;
		
	}
	
	void Compare(Student a,Student b)
	{
		if(a.totalScore>b.totalScore)
		{
			System.out.println(a.name+"'s total score is higher than "+b.name);
		}
		else if(a.totalScore<b.totalScore)
		{
			System.out.println(a.name+"'s total score is lower than "+b.name);
		}
		else
		{
			System.out.println(a.name+"'s total score is equal to "+b.name);
		}
	}


	
	
}

class StudentXW extends Student
{
	private String responsibility;
	
	public void setResponsibility(String responsibility)
	{
		this.responsibility=responsibility;
	}
	
	String getResponsibility()
	{
		return this.responsibility;
	}
	
	float testScore()
	{
		return super.getScore()+3;
	}
	
	
}

class StudentBZ extends Student
{
	private String responsibility;
	
	public void setResponsibility(String responsibility)
	{
		this.responsibility=responsibility;
	}
	
	String getResponsibility()
	{
		return this.responsibility;
	}
	
	float testScore()
	{
		return super.getScore()+5;
	}
	

}
public class StudentTest {
	
	public static void main(String args[])
	{
		Scanner cin=new Scanner(System.in);
	   Student [] tmp=new Student[10];//一个类里面只能调用一种形式!你数组这么用了
	   //取不同名字就行了
		//就不能再多态!
		for(int i=0;i<4;i++)
		{
			
			String number=cin.next();//这个字符串的读入空格也会读入!
			//所以并不能直接跳到下一个
			 String name=cin.next();
			//直接next读入单词,nextLine读入字符串
			 //也就是说。如果不想存入“ ”,一般来说就是一个单词,或者一连串非空字符
			 //就next够用了,特殊的才是有空字符的!
		     float englishScore=cin.nextFloat();//sb了。我float类型还打Int
		     float mathScore=cin.nextFloat();
		     float computerScore=cin.nextFloat();
		     String responsibility=cin.next();
		     //没输入是不会默认输入空格的,而是直接输入算到下一组的数据中
		     //因此此题的数据根本就不规范,只能直接写出来才行,当然我可以自己优化数据
		     if(responsibility=="Receivehomework")
		     {
		    	 englishScore+=3;
		    	 mathScore+=3;
		    	 computerScore+=3;
		     }
		     else if(responsibility==" Leadeveryone")
		     {
		    	 englishScore+=5;
		    	 mathScore+=5;
		    	 computerScore+=5;
		     }
		    	 
		     
		     
		     Student s=new Student(name,englishScore,mathScore,computerScore);
		     //不同形式的重载是没问题的
		     tmp[i]=s;
//			 s[i]=new Student(name,englishScore,mathScore,computerScore);不能这么一步到位
             //s[i]=new Studentdata(name,englishScore,mathScore,computerScore);
             //有同类名冲突,
		 // Student [] s=new Student[] {new Student(name,englishScore,mathScore,computerScore)};
			System.out.println(s.getName()+"'s evaluation score is "+s.getScore());
		

			
			
		}
//		Student s=new Student();//不能这么写,一旦给出方法以后就不能写成这样
	//必须传入参数,因此最简单就是随便用一个对象的Compare即可
		tmp[3].Compare(tmp[1],tmp[0]);//随便一个对象的compare都可以
		tmp[3].Compare(tmp[1],tmp[2]);//s不是变量,不能直接写成a,b
		tmp[3].Compare(tmp[1],tmp[3]);
	
		//总结就是对象数组加重载这种使用过于超前了。一般构造函数就使用变量就好了
		//这个方法可行但是版本不行就算了.
		//或者说学的太少,先放着积累经验先,遇到这个问题有人写出来再学,自己琢磨很困难.
	}
	
	//版本问题,对象数组重载当前版本没有这个方法就报错,算了。
	
	

}


//思考:每次都开创一个类,不重载多态就一个class,后面的全是方法.
//新建一个对象后加“,”直接使用.
//2.就基本来说,先存放set后获取提供使用get都是一起走的,基本是必备


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值