百度云盘链接:https://pan.baidu.com/s/1Ccfcfy_ybgIG88wmf3grQA
提取码:2eas
源码
1 package com.phone; 2 /** 3 * 手机类 4 * @author Administrator 5 * 2019-02-18 6 */ 7 public abstract class Phone { 8 private String brand; 9 private String type; 10 11 public abstract void call(); 12 13 public abstract void send(); 14 15 public abstract void info(); 16 }
1 package com.phone; 2 /** 3 * 播放 4 * @author Administrator 5 * 2019-02-18 6 */ 7 public interface Play { 8 public void bofang(); 9 }
1 package com.phone; 2 /** 3 * 拍照 4 * @author Administrator 5 * 2019-02-18 6 */ 7 public interface Photo { 8 public void paizhao(); 9 }
1 package com.phone; 2 /** 3 * 上网 4 * @author Administrator 5 * 2019-02-18 6 */ 7 public interface Online { 8 public void shangwang(); 9 }
1 package com.phone; 2 /** 3 * 普通手机 4 * @author Administrator 5 * 2019-02-18 6 */ 7 public class OldPhone extends Phone implements Play { 8 9 @Override 10 public void bofang() { 11 System.out.println("播放音频。"); 12 } 13 14 @Override 15 public void call() { 16 System.out.println("打电话。"); 17 } 18 19 @Override 20 public void send() { 21 System.out.println("发短信。"); 22 } 23 24 @Override 25 public void info() { 26 System.out.println("普通手机。"); 27 this.bofang(); 28 this.call(); 29 this.send(); 30 } 31 32 33 }
1 package com.phone; 2 /** 3 * 智能手机 4 * @author Administrator 5 * 2019-02-18 6 */ 7 public class NewPhone extends Phone implements Photo,Play,Online { 8 9 @Override 10 public void bofang() { 11 System.out.println("播放视频。"); 12 } 13 14 @Override 15 public void paizhao() { 16 System.out.println("拍照。"); 17 } 18 19 @Override 20 public void call() { 21 System.out.println("打电话。"); 22 } 23 24 @Override 25 public void send() { 26 System.out.println("发短息。"); 27 } 28 29 @Override 30 public void shangwang() { 31 System.out.println("上网。"); 32 } 33 34 @Override 35 public void info() { 36 System.out.println("智能手机。"); 37 this.bofang(); 38 this.call(); 39 this.paizhao(); 40 this.send(); 41 this.shangwang(); 42 } 43 44 }
1 package com.phone; 2 /** 3 * 测试类 4 * @author Administrator 5 * 2019-02-18 6 */ 7 public class Test { 8 public static void main(String[] args) { 9 OldPhone op = new OldPhone(); 10 op.info(); 11 System.out.println("---------"); 12 NewPhone np = new NewPhone(); 13 np.info(); 14 } 15 }
运行结果:
1 package com.cpu; 2 3 public interface CPU { 4 //品牌 5 public String type(); 6 //主频 7 public String Zhuping(); 8 }
1 package com.cpu; 2 3 public class Intel implements CPU { 4 5 @Override 6 public String type() { 7 8 return "Intel"; 9 } 10 11 @Override 12 public String Zhuping() { 13 14 return "4.0GHz"; 15 } 16 17 }
1 package com.cpu; 2 3 public class AMD implements CPU{ 4 5 @Override 6 public String type() { 7 8 return "AMD"; 9 } 10 11 @Override 12 public String Zhuping() { 13 14 return "4.0GHz"; 15 } 16 17 }
1 package com.ems; 2 3 public interface EMS { 4 //类型 5 public String type(); 6 //容量 7 public String capacity(); 8 }
1 package com.ems; 2 3 public class Samsung implements EMS { 4 5 @Override 6 public String type() { 7 8 return "三星"; 9 } 10 11 @Override 12 public String capacity() { 13 14 return "8GB"; 15 } 16 17 }
1 package com.ems; 2 3 public class Kingston implements EMS { 4 5 @Override 6 public String type() { 7 8 return "金士顿"; 9 } 10 11 @Override 12 public String capacity() { 13 14 return "8GB"; 15 } 16 17 }
package com.harddisk; public interface Harddisk { //容量 public String capacity(); }
1 package com.harddisk; 2 3 public class HarddiskDao implements Harddisk{ 4 5 @Override 6 public String capacity() { 7 8 return "1T"; 9 } 10 11 }
1 package com.computer; 2 3 import com.cpu.CPU; 4 import com.ems.EMS; 5 import com.harddisk.Harddisk; 6 7 public class Computer { 8 private CPU cpu; 9 private EMS ems; 10 private Harddisk hd; 11 12 public Computer() { 13 super(); 14 15 } 16 17 public Computer(CPU cpu, EMS ems, Harddisk hd) { 18 super(); 19 this.cpu = cpu; 20 this.ems = ems; 21 this.hd = hd; 22 } 23 24 public CPU getCpu() { 25 return cpu; 26 } 27 28 public void setCpu(CPU cpu) { 29 this.cpu = cpu; 30 } 31 32 public EMS getEms() { 33 return ems; 34 } 35 36 public void setEms(EMS ems) { 37 this.ems = ems; 38 } 39 40 public Harddisk getHd() { 41 return hd; 42 } 43 44 public void setHd(Harddisk hd) { 45 this.hd = hd; 46 } 47 48 public void info() { 49 System.out.println("计算机的信息如下:"); 50 System.out.println("cpu的品牌:" + cpu.type() + ", 主频:" + cpu.Zhuping()); 51 System.out.println("ems的类型:" + ems.type() + ", 容量:" + ems.capacity()); 52 System.out.println("硬盘的容量:" + hd.capacity()); 53 } 54 }
1 package com.computer; 2 3 import com.cpu.AMD; 4 import com.cpu.Intel; 5 import com.ems.Kingston; 6 import com.ems.Samsung; 7 import com.harddisk.HarddiskDao; 8 9 public class Test { 10 public static void main(String[] args) { 11 //Intel 金士顿 1T 12 Computer cpt1 = new Computer(new Intel(),new Kingston(),new HarddiskDao()); 13 cpt1.info(); 14 System.out.println("**********"); 15 Computer cpt2 = new Computer(); 16 cpt2.setCpu(new AMD()); 17 cpt2.setEms(new Samsung()); 18 cpt2.setHd(new HarddiskDao()); 19 cpt2.info(); 20 21 } 22 }
运行结果:
来源:https://www.cnblogs.com/fanxinmu/p/10394743.html