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); } }
时间: 2024-10-26 21:26:54