代码片--可扩展性的模式(接口加多态)
package com.dreamy.day03; /** * @author dreamy * 需求: * 电脑运行实例 * 电脑运行基于主板 * 用接口和多态实现程序的可扩展性 */ abstract interface PCI{ public abstract void open(); public abstract void close(); } class MainBoard{ public void run() { System.out.println("mainboard run..."); } public void userPCI(PCI p) {//PCI p=new NetCard//接口型引用指向自己的子类对象--多态的应用 if(p!=null) { p.open(); p.close(); } } } public class DuoTai02 { /** * @param args */ public static void main(String[] args) { MainBoard mb=new MainBoard(); mb.run(); mb.userPCI(null); mb.userPCI(new NetCard()); mb.userPCI(new SoundCard()); } } class NetCard implements PCI{ @Override public void open() { System.out.println("netcard open"); } @Override public void close() { System.out.println("netcard close"); } } class SoundCard implements PCI{ @Override public void open() { System.out.println("soundcard open"); } @Override public void close() { System.out.println("soundcard close"); } }