2、设计一个人员类(Person),其中包含一个方法pay,代表人员的工资支出。再从Person类派生出教师类(Teacher)和大学生类(CollegeStudent)其中
教师:工资支出=基本工资+授课时数*30
大学生:奖学金支出
将人员类定义为抽象类,pay为抽象方法,设计程序实现多态性。
3、将第2题中的pay定义在接口中,设计程序实现多态性。
4、为上述3题设计验证的主类。
上面3道题目就是,我知道这是比较简单的,可是我主要学的是C 对JAVA不太了解 没有到这么一道题目居然写不出来,希望各位朋友帮忙给些出个可以运行的代码,谢谢。题目完成 另开帖加100分~ 谢谢哈。
{
public void pay();
}
然后让类去实现这个接口public class teacher implements ttt{
public void pay()
{}
}
public abstract double pay();}package cn.tangxin.pojo;public class Teacher extends Person implements Common{
public Teacher(){}
public Teacher(double basePay,int hour){
this.basePay = basePay;
this.hour = hour;
}
private double basePay; public double getBasePay() {
return basePay;
} public void setBasePay(double basePay) {
this.basePay = basePay;
}
private int hour; public int getHour() {
return hour;
} public void setHour(int hour) {
this.hour = hour;
} @Override
public double pay() {
return basePay+hour*30;
} public double pay2() {
return pay();
}}package cn.tangxin.pojo;public class CollegeStudent extends Person implements Common{
public CollegeStudent(){}
public CollegeStudent(double scholarship){
this.scholarship = scholarship;
}
private double scholarship; public double getScholarship() {
return scholarship;
} public void setScholarship(double scholarship) {
this.scholarship = scholarship;
} @Override
public double pay() {
return this.scholarship;
} public double pay2() {
return pay();
}}package cn.tangxin.pojo;public interface Common {
public double pay2();
}package cn.tangxin.pojo;public class Test1 {
public static void main(String[] args) {
//多态性
Person teacher = new Teacher(3000,36);
System.out.println(teacher.pay());
Person collegeStudent = new CollegeStudent(5000);
System.out.println(collegeStudent.pay());
//使用接口的多态性
Common teacherCommon = new Teacher(3000,36);
System.out.println(teacherCommon.pay2());
Common collegeStudentCommon = new CollegeStudent();
System.out.println(collegeStudentCommon.pay2());
}}
abstract class Person{
public abstract void pay();
}
class Teacher extends Person{
private double Bsalary;
private double Paysalary;
Teacher(double Bsalary)
{
this.Bsalary=Bsalary;
}
public double Rellaysalary(int p)
{
this.Paysalary=Bsalary+p*30;
return this.Paysalary;
}
public void setBsalary(double j)
{
this.Bsalary=j;
}
public double getSalary()
{
return this.Paysalary;
}
public void pay()
{
System.out.println("教师的实际支出工资为:"+getSalary());
}
}
class CollegeStudent extends Person{
private double Collegeship;
CollegeStudent(double Collegeship)
{
this.Collegeship=Collegeship;
}
public double getCollegeship(){
return Collegeship;
}
public void setCollegeship(double i)
{
this.Collegeship=i;
}
public void pay()
{
System.out.println("大学生的奖金支出为:"+getCollegeship());
}
}
public class Test{
public static void main(String[] args){
Teacher t=new Teacher(2000);
CollegeStudent cs=new CollegeStudent(2500);
//t.setBsalary(3000); 重新设置老师的基本工资;
//cs.setCollegeship(3500); 重新设置大学生的奖金数目;
t.Rellaysalary(30);
t.pay();
cs.pay();
}
}
这个应该就可以了
import java.util.*;
//定义人员类
abstract class Person
{
public abstract double pay();
}
class Teacher extends Person
{
double Bsalary;
int p;
double Paysalary;
//构造函数
//Teacher(){}
public double pay()
{
return Rellaysalary();
}
public double Rellaysalary()
{
this.Paysalary=Bsalary+this.p*30;
return this.Paysalary;
}
public void setBsalary(double j)
{
this.Bsalary=j;
}
public double getSalary()
{
return this.Paysalary;
}
}
class CollegeStudent extends Person
{
private double Collegeship;
CollegeStudent(double Collegeship)
{
this.Collegeship=Collegeship;
}
double getCollegeship()
{
return Collegeship;
}
void setCollegeship(double i)
{
this.Collegeship=i;
}
public double pay()
{
return getCollegeship();
}
}
public class Test{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("请输入教师的基本工资:");
Teacher teacher=new Teacher();
teacher.Bsalary=in.nextDouble();
System.out.println("请输入教师的授课学时:");
teacher.p=in.nextInt();
System.out.println("请输入学生的奖学金金额" +
":");
CollegeStudent student=new CollegeStudent(in.nextDouble());
System.out.println("教师的工资:"+teacher.Rellaysalary());
System.out.println("学生的奖学金:"+student.pay());
}
}