学习笔记之动态多态

public interface Ipower {
    public abstract void tigongdianyuan();
}

 动态多态:指系统A访问系统B的服务时,系统B可以通过多种实现来提供服务,而这一切对于A来说都是透明的

 

public class ACPower implements Ipower {

    
    public void tigongdianyuan() {
        System.out.println("适配器提供电源");

    }

}

 

 

public class Barry implements Ipower {


    public void tigongdianyuan() {
        System.out.println("电池提供电源");

    }

}

 

 

public class Computer {
    
    public void kaiJi(Ipower pow){
        pow.tigongdianyuan();
        System.out.println("开机了");
    }
    
}

 

 

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Computer c = new Computer();
        ACPower ac = new ACPower();
        Barry ba = new Barry();
        c.kaiJi(ba);
    }

}

 

posted on 2015-10-22 19:27  托马斯不拖马  阅读(141)  评论(0编辑  收藏  举报

导航