父类手机类:
1 package com.phone; 2 3 public abstract class Phone { 4 public String brand; 5 public String type; 6 7 public Phone(String brand, String type) { 8 super(); 9 this.brand = brand; 10 this.type = type; 11 } 12 13 public abstract void send(); 14 public abstract void call(); 15 16 }
拍照接口:
1 package com.phone; 2 3 public interface Photo { 4 public void photo(); 5 }
音频接口:
1 package com.phone; 2 3 public interface Music { 4 public void music(); 5 }
播放接口:
1 package com.phone; 2 3 public interface Play { 4 public void play(); 5 }
上网接口:
1 package com.phone; 2 3 public interface Inter { 4 public void inter(); 5 6 }
普通手机:
1 package com.phone; 2 3 public class CommonPhone extends Phone implements Music{ 4 5 6 public CommonPhone(String brand, String type) { 7 super(brand, type); 8 // TODO Auto-generated constructor stub 9 } 10 11 public void send() { 12 System.out.println("发短信"); 13 14 } 15 16 public void call() { 17 System.out.println("打电话"); 18 } 19 20 @Override 21 public void music() { 22 // TODO Auto-generated method stub 23 System.out.println("播放音频"); 24 25 } 26 27 }
智能手机:
1 package com.phone; 2 3 public class ZhiNengPhone extends Phone implements Music,Photo,Play,Inter{ 4 5 public ZhiNengPhone(String brand, String type) { 6 super(brand, type); 7 // TODO Auto-generated constructor stub 8 } 9 10 @Override 11 public void send() { 12 // TODO Auto-generated method stub 13 System.out.println("发短信"); 14 } 15 16 @Override 17 public void call() { 18 // TODO Auto-generated method stub 19 System.out.println("打电话"); 20 } 21 22 @Override 23 public void inter() { 24 // TODO Auto-generated method stub 25 System.out.println("上网"); 26 } 27 28 @Override 29 public void play() { 30 // TODO Auto-generated method stub 31 System.out.println("播放"); 32 } 33 34 @Override 35 public void photo() { 36 // TODO Auto-generated method stub 37 System.out.println("拍照"); 38 } 39 40 @Override 41 public void music() { 42 // TODO Auto-generated method stub 43 System.out.println("播放音频"); 44 } 45 46 }
测试类:
1 package com.phone; 2 3 public class Test { 4 public static void main(String[] args) { 5 CommonPhone phone = new CommonPhone("诺基亚", "A3"); 6 System.out.println("这是一部"+phone.brand+",型号为"+phone.type); 7 phone.send(); 8 phone.call(); 9 phone.music(); 10 System.out.println("****************"); 11 ZhiNengPhone zn = new ZhiNengPhone("苹果", "6s"); 12 System.out.println("这是一部"+zn.brand+",型号为"+zn.type); 13 zn.call(); 14 zn.send(); 15 zn.inter(); 16 zn.music(); 17 zn.photo(); 18 zn.play(); 19 } 20 }
输出:
原文地址:https://www.cnblogs.com/Zhangchuanfeng1/p/10395952.html
时间: 2024-10-03 06:12:49