- /*
- 需求:
- 电脑运行实例
- 电脑运行基于主板
- */
- class MainBoard//主板
- {
- public void run()
- {
- System.out.println("mainboad run");
- }
- public void usePCI(PCI p)
- {
- if(p!=null)
- {
- p.open();
- p.close();
- }
- }
- }
- interface PCI//创建接口,实现功能扩展 PCI P=newSoundCard()/接口型引用指向自己的子类对象
- {
- public void open();
- public void close();
- }
- class SoundCard implements PCI//实现声卡
- {
- public void open()
- {
- System.out.println("SoundCard open");
- }
- public void close()
- {
- System.out.println("SoundCard close");
- }
- }
- class DuoTai1
- {
- public static void main(String[] args)
- {
- MainBoard mb=new MainBoard();
- mb.run();
- mb.usePCI(new SoundCard());
- }
- }
复制代码
|
|