java程序设计

一***五个类

首先,人物类,person ,Teacher类和CollegeStudent类继承之。

其次,动作类pay接口,person可以不实现,因为他是抽象类,Teacher类和CollegeStudent类要implements这个接口

最后是测试类

下面是代码

person类:

public abstract class Person implements Pay{}

Teacher类:

public class Teacher extends Person {

public void pay() {

System.out.println("基本工资+授课时数*30");

}

}

CollegeStudent类:

public class CollegeStudent extends Person {

public void pay() {

System.out.println("奖学金支出");

}

}

pay接口:

public interface Pay {

public void pay();

}

测试类:

public class Test {

public static void main(String[] args) {

Person teacher=new Teacher();//左父右子

Person collegeStudent=new CollegeStudent();//左父右子

teacher.pay();//调用的还是person类型,由于是动态绑定,所以多态成老师的方法

collegeStudent.pay();// 和上面 类似

}

}