多态 接口
Posted 宫崎天川
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多态 接口相关的知识,希望对你有一定的参考价值。
1 package PC; 2 3 public class computer { 4 5 private String pinpai; 6 7 private ICPU cpu; 8 9 private Idisplaycard displaycard; 10 11 public String getPinpai() { 12 return pinpai; 13 } 14 15 public void setPinpai(String pinpai) { 16 this.pinpai = pinpai; 17 } 18 19 public ICPU getCpu() { 20 return cpu; 21 } 22 23 public void setCpu(ICPU cpu) { 24 this.cpu = cpu; 25 } 26 27 public Idisplaycard getDisplaycard() { 28 return displaycard; 29 } 30 31 public void setDisplaycard(Idisplaycard displaycard) { 32 this.displaycard = displaycard; 33 } 34 35 public void run() 36 { 37 this.cpu.command(); 38 39 this.displaycard.display(); 40 41 System.out.println("我组装的"+pinpai+"电脑开始运行"); 42 } 43 public computer(String pinpai) { 44 super(); 45 this.pinpai = pinpai; 46 this.cpu = cpu; 47 this.displaycard = displaycard; 48 } 49 50 public static void main(String []args) 51 { 52 System.out.println(); 53 computer c1 = new computer("hp"); 54 AMD a1 = new AMD(); 55 c1.setCpu(a1); 56 c1.setDisplaycard(new Nivdia()); 57 c1.run(); 58 59 System.out.println(); 60 computer c2 = new computer("华硕"); 61 c2.setCpu(new intel()); 62 c2.setDisplaycard(new Nivdia()); 63 c2.run(); 64 65 System.out.println(); 66 AMD2 a2 = new AMD2(); 67 computer c3 = new computer("acer"); 68 c3.setCpu(a2); 69 c3.setDisplaycard(a2); 70 c3.run(); 71 72 System.out.println(); 73 AMD3 a3 = new AMD3(); 74 computer c4 = new computer("acer"); 75 c4.setCpu(a3); 76 c4.setDisplaycard(a3); 77 c4.run(); 78 } 79 }
1 package PC; 2 3 public class ATI implements Idisplaycard { 4 5 @Override 6 public void display() { 7 8 System.out.println("我是A卡"); 9 } 10 }
1 package PC; 2 3 public class AMD implements ICPU { 4 5 @Override 6 public void command() { 7 8 System.out.println("我是AMD的CPU"); 9 10 } 11 }
以上是关于多态 接口的主要内容,如果未能解决你的问题,请参考以下文章