【Java接口实现动态加载不同的类】

public interface Person {    
   public double calcuMonthlySalary(double sal, int type);    
 }  
public class Manager implements Person {    
     public Manager () {        }    
    public double calcuMonthlySalary(double sal, int type) {    
           System.out.println("Manager: "+(sal*type*3));    
            return sal*type*2;    
    }    
}    
public class Employee implements Person {    
    public Employee () {        }       
     public double calcuMonthlySalary(double sal, int type) {    
        System.out.println("Employee: "+(sal*type*2));    
        return sal*type*1.5;    
    }   
}    
public class SalaryManageTest {       
       public static void main(String[] args) throws Exception{    
   
        Person personService = (Person)Class.forName(args[0]).newInstance(); 
     //Class.forName(类名)可以动态加载某个类,比如在控制台中Java SalaryManageTest Manager 1000 3,即可动态加载Manager类,args[1]=1000,args[2]=3.               
if (args[1] != null && args[1] != "" &&                        args[2] != null && args[2] != "") {                    int sal = Integer.valueOf(args[1]).intValue();                    int type = Integer.valueOf(args[2]).intValue();            //这里可以根据传入参数的不同,调用不同的实现类,                //如果再增加一类新的成员,也只要新增一个类,无需修改方法                    personService.calcuMonthlySalary(sal, type);                }                 }       }  

接口的作用?

(1)通过接口实现不相关类的相同行为,而无需考虑这些类之间的关系.
 
(2)通过接口指明多个类需要实现的方法
 
(3)利用接口的多态性,动态加载不同类的实例
 
原文地址:https://www.cnblogs.com/4everlove/p/3383590.html