1 package com.ketang.phone; 2 3 /** 4 * 父类:手机类 5 * @author 6 * 7 */ 8 public abstract class Phone { 9 private String brand; //品牌 10 private String type; //型号 11 12 public Phone() { 13 14 } 15 16 public Phone(String brand, String type) { 17 super(); 18 this.brand = brand; 19 this.type = type; 20 } 21 22 public String getBrand() { 23 return brand; 24 } 25 26 public void setBrand(String brand) { 27 this.brand = brand; 28 } 29 public String getType() { 30 return type; 31 } 32 public void setType(String type) { 33 this.type = type; 34 } 35 36 //发短信 37 public abstract void sendMess() ; 38 39 //通电话 40 public abstract void call(); 41 42 public void showInfo(){ 43 System.out.println("这是一部"+brand+type+"手机"); 44 } 45 46 }
1 package com.ketang.phone; 2 3 /** 4 * 接口:上网 5 * @author 6 * 7 */ 8 public interface NetWorking { 9 void netWorking(); 10 }
1 package com.ketang.phone; 2 3 /** 4 * 接口:播放音乐 5 * @author 6 * 7 */ 8 public interface PlayMusic { 9 void playMusic(String name); 10 }
1 package com.ketang.phone; 2 3 /** 4 * 接口:播放视频 5 * @author 6 * 7 */ 8 public interface PlayVideo { 9 void playVideo(String name); 10 }
1 package com.ketang.phone; 2 /** 3 * 接口:拍照 4 * @author 5 * 6 */ 7 public interface TakePhoto { 8 void takePhoto(); 9 }
1 package com.ketang.phone; 2 3 /** 4 *子类: 普通手机类,继承手机类,拥有播放音乐接口 5 * @author 6 * 7 */ 8 public class CommonPhone extends Phone implements PlayMusic{ 9 10 public CommonPhone() { 11 super(); 12 } 13 14 public CommonPhone(String brand, String type) { 15 super(brand, type); 16 } 17 18 //播放音频 19 public void playMusic(String name) { 20 System.out.println("播放音频《"+name+"》"); 21 } 22 23 //发短信 24 public void sendMess() { 25 System.out.println("发短信"); 26 } 27 28 //通电话 29 public void call() { 30 System.out.println("打电话"); 31 } 32 33 34 35 }
1 package com.ketang.phone; 2 3 /** 4 * 子类:智能手机类,继承手机类,拥有播放视频、上网、拍照的接口 5 * @author 6 * 7 */ 8 public class ZhinengPhone extends Phone implements PlayVideo,NetWorking,TakePhoto { 9 10 public ZhinengPhone() { 11 super(); 12 } 13 14 public ZhinengPhone(String brand, String type) { 15 super(brand, type); 16 } 17 18 //发短信 19 public void sendMess() { 20 System.out.println("发送图片+文字+视频信息"); 21 } 22 23 //通话 24 public void call() { 25 System.out.println("视频通话"); 26 } 27 28 //拍照 29 public void takePhoto() { 30 System.out.println("拍照"); 31 } 32 33 //播放视频 34 public void playVideo(String name) { 35 System.out.println("播放视频《"+name+"》"); 36 } 37 38 //上网 39 public void netWorking() { 40 System.out.println("打开网络,开始上网"); 41 } 42 }
1 package com.ketang.phone; 2 3 public class Test { 4 public static void main(String[] args) { 5 CommonPhone cp=new CommonPhone("索尼爱立信","G501"); 6 cp.showInfo(); 7 cp.call(); 8 cp.sendMess(); 9 cp.playMusic("南山南"); 10 System.out.println("********************************"); 11 12 ZhinengPhone zp=new ZhinengPhone("iPhone"," XMax"); 13 zp.showInfo(); 14 zp.call(); 15 zp.sendMess(); 16 zp.netWorking(); 17 zp.playVideo("琅琊榜"); 18 zp.takePhoto(); 19 20 21 } 22 }