1 通过抽象类实现,通过对抽象类中的抽象方法不同的实现,来实现多态。
package unit4; public abstract class Employee { int basic =2000; abstract void salary(); } package unit4; public class Manager extends Employee { void salary(){ System.out.println("薪资等于:"+basic*5); System.out.println("**********************"); } } package unit4; public class Worker extends Employee { void salary(){ System.out.println("薪资等于:"+basic*2); System.out.println("***********************"); } } package unit4; import com.sun.java_cup.internal.parse_action; public class abstracttest { public static void main(String[] args) { Employee[] p = new Employee[5]; p[0]=new Manager(); p[1]= new Worker(); p[2]=new Manager(); p[3]= new Worker(); p[4]=new Manager(); p[2]= new Worker(); for(int i=0;i<5;i++){ p[i].salary(); } } }