1.防盗门模型
手机类:Handset 普通手机类:CommonHandset 智能手机类:AptitudenHandst 测试类:Test
照相接口:TheakePictures 播放接口:PlayWiring 链接网络接口:Network
手机类:Handset
// 继承 重写
public abstract class Handset {
//属性
private String brand;
private String type;
public void sendInfo(){
System.out.println("这是一款型号为"+type+"的"+brand+"手机");
}
//抽象方法
public abstract void call();
public abstract void info();
//属性封装
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;
}
}
普通手机类:CommonHandset
public class CommonHandset extends Handset implements PlayWiring{
@Override
public void play(String content) {
System.out.println("开始播放音乐《"+content+"》");
}
public void call(){
System.out.println("开始语音通话.....");
}
public void info(){
System.out.println("开始发送文字信息.....");
}
}
智能手机类:AptitudenHandst
public class AptitudeHandst extends Handset implements TheakePictures,Network,PlayWiring{
@Override
public void networkConn() {
System.out.println("已经启动移动网络......");
}
@Override
public void takePictare() {
System.out.println("咔嚓......拍照成功");
}
@Override
public void play(String content) {
System.out.println("开始播放视频《"+content+"》");
}
public void call(){
System.out.println("开始视频通话.....");
}
public void info(){
System.out.println("开始发送带图片与文字信息.....");
}
}
照相接口:TheakePictures
public interface TheakePictures {
void takePictare();
}
播放接口:PlayWiring
public interface PlayWiring {
void play(String content);
}
链接网络接口:Network
public interface Network {
void networkConn();
}
测试类:Test
public class Test {
public static void main(String[] args) {
CommonHandset c = new CommonHandset();
c.setType("G502c");
c.setBrand("索尼爱立信");
c.sendInfo();
c.play("热雪");
c.info();
c.call();
System.out.println("------------------");
AptitudeHandst a = new AptitudeHandst();
a.setType("HTC");
a.setBrand("I9100");
a.sendInfo();
a.networkConn();
a.play("小时代");
a.takePictare();
a.info();
a.call();
}
}
2.打印机模型
例题:需求说明:采用面向接口编程思想组装一台计算机,计算机的主要组成部分有:CPU、硬盘、内存:首先,定义CPU的接口CPU,返回CPU品牌和主频;其次,定义内存的接口EMS,返回容量;再次,定义硬盘的接口HardDisk,返回容量;然后,编写各组件厂商分别实现CPU、EMS、和HardDisk接口,编写计算机类,组装计算机并显示相关信息;最后,编写测试类运行如图所示的效果。
把实现类放入impl文件夹:Cpuimpl Emsimpl HardDiskimpl
计算机类:Computer 测试类TestComputer
接口:Cpu Ems HardDisk
Cpu接口:
public interface Cpu {
String type();
String hz();
}
Ems接口:
public interface Ems {
int size();
}
HardDisk接口:
public interface HardDisk {
int size();
}
计算机类:Computer:
public class Computer {
//接口作为成员属性并封装
private Cpu cpu;
private Ems ems;
private HardDisk harddisk;
public void show(){
System.out.println("计算机的信息如下:");
System.out.println("CPU的品牌是:"+cpu.type()+",主频是:"+cpu.hz());
System.out.println("硬盘的容量是:"+harddisk.size()+"GB");
System.out.println("内存容量是:"+ems.size()+"GB");
}
public Cpu getCpu() {
return cpu;
}
public void setCpu(Cpu cpu) {
this.cpu = cpu;
}
public Ems getEms() {
return ems;
}
public void setEms(Ems ems) {
this.ems = ems;
}
public HardDisk getHarddisk() {
return harddisk;
}
public void setHarddisk(HardDisk harddisk) {
this.harddisk = harddisk;
}
}
impl文件夹:Cpuimpl Emsimpl HardDiskimpl
public class Cpuimpl implements Cpu {
@Override
public String type() {
return "Intel";
}
@Override
public String hz() {
return "3.8GHz";
}
}
public class Emsimpl implements Ems {
@Override
public int size() {
return 4;
}
}
public class HardDiskimpl implements HardDisk {
@Override
public int size() {
return 3000;
}
}
测试类TestComputer :
import com.computer1.impl.Cpuimpl;
import com.computer1.impl.Emsimpl;
import com.computer1.impl.HardDiskimpl;
public class TestComputer {
public static void main(String[] args) {
Computer c = new Computer();
c.setCpu(new Cpuimpl());
c.setHarddisk(new HardDiskimpl());
c.setEms(new Emsimpl());
c.show();
}
}