作业一、接口实现手机
原始的手机,可以发短信,通电话。随着发展,手机增加了功能:音频、视频播放、拍照、上网。使用接口实现手机功能;
package com.phone.jiekou; public abstract class HandSet { private String brand; private String type; public HandSet() {} public HandSet(String brand,String type) { this.brand = brand; this.type = type; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getType() { return type; } public void setType(String type) { this.type = type; } public abstract void message(); public abstract void call();
创建手机功能接口:
1、上网功能接口:
package com.phone.jiekou; public interface Network { public void net(); }
2、拍照功能接口:
package com.phone.jiekou; public interface Photo { public void takePhoto(); }
3、播放视频功能接口:
package com.phone.jiekou; public interface Play { public void play(String film); }
4、播放音频功能接口:
package com.phone.jiekou; public interface Music { public void boFang(String music); }
创建普通手机类要求拥有播放音频功能:
package com.phone.jiekou; public class CommonPhone extends HandSet implements Music{ public CommonPhone() { } public CommonPhone(String brand,String type) { super(brand,type); } public void showInfo() { System.out.println("手机信息及功能介绍:"); System.out.println("这是一部"+this.getBrand()+"的手机,型号是"+this.getType()); } public void message() { System.out.println("发送短信!"); } public void call() { System.out.println("拨打电话!"); } public void boFang(String music) { System.out.println("可以播放"+music+"!"); } }
创建智能手机类要求拥有上网功能、拍照功能、播放视频功能
package com.phone.jiekou; public class SmartPhone extends HandSet implements Network, Photo, Play { public SmartPhone() {} public SmartPhone(String brand,String type) { super(brand,type); } public void showInfo() { System.out.println("手机信息及功能介绍:"); System.out.println("这是一部"+this.getBrand()+"的手机,型号是"+this.getType()); } public void play(String film) { System.out.println("播放电影"+film); } public void takePhoto() { System.out.println("拍照功能"); } public void net() { System.out.println("上网功能"); } public void message() { System.out.println("发送短信"); } public void call() { System.out.println("拨打电话"); } }
编写测试类:
package com.phone.jiekou; public class TestPhone { public static void main(String[] args) { CommonPhone cphone = new CommonPhone("三星","I3452"); cphone.showInfo(); cphone.message(); cphone.call(); cphone.boFang("音乐"); SmartPhone sphone = new SmartPhone("苹果","6P"); sphone.showInfo(); sphone.message(); sphone.call(); sphone.play("大话西游"); sphone.net(); sphone.takePhoto(); } }
实现结果为:
手机信息及功能介绍: 这是一部三星的手机,型号是I3452 发送短信! 拨打电话! 可以播放音乐! 手机信息及功能介绍: 这是一部苹果的手机,型号是6P 发送短信 拨打电话 播放电影大话西游 上网功能 拍照功能