题目:原始的手机,可以发短信,通电话。随着发展,手机增加了功能:音频、视频播放、拍照、上网 试用程序模拟
代码实现:
package phone;public class Handset {
private String brand;
private String type;
public Handset(String brand,String type) {super();this.brand= brand;this.type=type;}
public void sendInfo() {System.out.println("发送文字信息.......");
}
public void call() {System.out.println("开始语音通话.......");
}
public void supcall() {System.out.println("开始视频通话.......");
}
public void info() {System.out.println("发送带图片的文字信息.......");
}
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;
}
@Override
public int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((brand == null) ? 0 : brand.hashCode());result = prime * result + ((type == null) ? 0 : type.hashCode());return result;
}@Override
public String toString() {return "这是一款型号为" + brand + "的" + type + "手机";
}
}
package phone;public class CommonHandset extends Handset implements PlayWiring{
public CommonHandset(String brand, String type) {super(brand, type);// TODO 自动生成的构造函数存根}
public void play() {System.out.println("开始播放音乐《热雪》");
}
public void gongneng() {Handset o1=new Handset(null, null);o1.sendInfo();o1.call();
}
}
package phone;public class AptitudeHandset extends Handset implements Network,TheakePictures,PlayWiring{
public AptitudeHandset(String brand, String type) {super(brand, type);// TODO 自动生成的构造函数存根}
public void gongneng() {Handset o1 =new Handset(null,null);o1.info();o1.supcall();
}public void networkConn() {System.out.println("移动数据正在启动..........");
}
public void takePictures(){System.out.println("咔嚓.......拍照成功");
}
public void play() {System.out.println("开始播放视频《小时代》");
}
}
package phone;public interface Network {
void networkConn();
}
package phone;public interface PlayWiring {public static void play(){}
}
package phone;public interface TheakePictures {
void takePictures();
}
package phone;
public class Test {public static void main(String[] args) {// TODO 自动生成的方法存根
CommonHandset bbb=new CommonHandset("G502C","索尼爱立信");
AptitudeHandset aaa=new AptitudeHandset("HTC","I9100");System.out.println(bbb);bbb.play();bbb.gongneng();System.out.println("------------------------");System.out.println(aaa);aaa.networkConn();aaa.takePictures();aaa.play();aaa.gongneng();
}}
运行结果:
这是一款型号为G502C的索尼爱立信手机
开始播放音乐《热雪》
发送文字信息…
开始语音通话…
这是一款型号为HTC的I9100手机
移动数据正在启动…
咔嚓…拍照成功
开始播放视频《小时代》
发送带图片的文字信息…
开始视频通话…