- interface PCI{
- void open();
- void close();
- }
- class NetCard implements PCI{
- public void open(){
- System.out.println("NetCard open");
- }
- public void close(){
- System.out.println("NetCard close");
- }
- }
- class SoundCard implements PCI{
- public void open(){
- System.out.println("SoundCard open");
- }
- public void close(){
- System.out.println("SoundCard close");
- }
- }
- class MainBoard{
- public static void run(){
- System.out.println("MainBoard run");
- }
- //PCI p = new NetCard(); 接口型引用指向自己的子类对象。
- public static void usePCI(PCI p){
- if(!(p==null)){
- p.open();
- p.close();
- }
- }
- }
- public class ComputerDemo {
- public static void main(String[] args){
- MainBoard m = new MainBoard();
- m.run();
- m.usePCI(new NetCard());
- m.usePCI(new SoundCard());
- }
- }
复制代码 |
|