首先,人物類,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();// 和上面 類似
}
}