1、java中编写一个类Teacher,描述教师的课时数量和计算课时的系数,均为double类型。

2025-01-24 05:39:27
推荐回答(1个)
回答1:

package com.demo;
public class Teacher {
private static double xishu=1.2;//课时数量,定义成static类型保证有教师的课时系数都相同
private double classNo=0.0;//课时数量
public Teacher(){
}
public Teacher(double classNo){//构造函数
this.classNo=classNo;
}
public double courseCompute( ){
return xishu*classNo;
}

public static void main(String[] string){
Teacher teacherOne=new Teacher(4);//申明一个四节课时的教师
Teacher teacherTwo=new Teacher(5);//申明一个四节课时的教师
System.out.println(teacherOne.courseCompute());//输出结果
System.out.println(teacherTwo.courseCompute());

teacherOne.xishu=1;//修改系数

System.out.println(teacherOne.courseCompute());//重新输出结果
System.out.println(teacherTwo.courseCompute());
}
}
运行结果: