抽象类和接口-手机小案例
生活随笔
收集整理的這篇文章主要介紹了
抽象类和接口-手机小案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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 }
?
轉載于:https://www.cnblogs.com/baichang/p/10067591.html
總結
以上是生活随笔為你收集整理的抽象类和接口-手机小案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL删除一个数据库内所有表的数据保留表
- 下一篇: 《搬砖日记》Obi Rope插件的简单使