abstract class Student{ String name; int fee; Student(String name){ this.name=name; } abstract void setFee(int fee); abstract int getFee();} interface Salary{ int getSalary();} class Graduate extends Student implements Salary{ int salary; Graduate(String name){ super(name); } void setFee(int fee){ this.fee=fee; } int getFee(){ return fee; } public int getSalary(){ this.salary+=1500; return salary; } boolean isLoan(){ if(salary-fee>0) return false; else return true; } }class Teacher implements Salary{ String name; int salary; Teacher(String name){ this.name=name; } public int getSalary(){ this.salary+=3000; return salary; } }class University{ void payOff(Salary s){ s.getSalary(); }}public class Test{ public static void main(String args[]){ Graduate g=new Graduate("小梁"); g.setFee(1000); University u =new University(); u.payOff(g); System.out.println("是否需要贷款 "+g.isLoan()); Teacher t=new Teacher("大王"); u.payOff(t); }}
课堂练习1:教材160页4.编程题
interface Animal{void cry();String getAnimalName();}class Simulator{ void playSound(Animal animal){ animal.cry(); } void getAnimalName(Animal animal){ System.out.println(animal.getAnimalName()); }}class Cat implements Animal{ String name="猫"; public void cry(){ System.out.println("猫"); } public String getAnimalName(){ return this.name;}}class Dog implements Animal{ String name="狗"; public void cry(){ System.out.println("狗"); } public String getAnimalName(){ return this.name;}}public class Application{ public static void main(String args[]){ Simulator simulator=new Simulator(); simulator.playSound(new Dog()); simulator.playSound(new Cat()); }}