抽象类与接口
目录
1.利用接口实现手机
接口
类图
测试类
2.打印机
接口
类
测试类:
3.组装计算机
接口:
类
测试类:
4.防盗门问题
接口:
类
测试类
5.利用接口实现简单计算
接口
类
测试类
编写测试类,让普通手机播放音频、发信息和通电话,让智能手机上网、播放视频、照相、发信息和通电话。
package dh06.phone;
//接口:上网
public interface NetWork {
void netWorkConn();
}
package dh06.phone;
//接口:播放能力
public interface PlaySth {
void play(String content);
}
package dh06.phone;
//接口:拍照能力
public interface TakePhoto {
void takePhotos();
}
父类:
package dh06.phone;
//抽象类--手机类
public abstract class Phone {
private String brand;// 品牌
private String type;// 型号
public Phone() {
}
public Phone(String brand, String type) {
this.brand = brand;
this.type = type;
}
// 输出手机信息
public void showInfo() {
System.out.println("这是一台" + brand + type + "手机");
}
// 打电话
public abstract void call();
// 发信息
public abstract void sendMsg();
//-------------------------------------------
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;
}
//-------------------------------------------
}
子类:
package dh06.phone;
//原始的普通手机,只有播放功能
public class CommonPhone extends Phone implements PlaySth {
public CommonPhone() {
}
public CommonPhone(String brand, String type) {
super(brand, type);
}
public void play(String content) {
System.out.println("正在播放《" + content + "》");
}
public void call() {
System.out.println("正在语音通话。。。。。。。。。。");
}
public void sendMsg() {
System.out.println("正在发送信息。。。。。。");
}
}
package dh06.phone;
//智能手机:具备上网、拍照、播放能力
public class SmartPhone extends Phone implements NetWork, TakePhoto, PlaySth {
public SmartPhone() {
}
public SmartPhone(String brand, String type) {
super(brand, type);
}
//播放音频和视频
public void play(String content) {
System.out.println("正在播放《" + content + "》");
}
//拍照
public void takePhotos() {
System.out.println("卡擦,拍了一张美图。。。。。");
}
//上网
public void netWorkConn() {
System.out.println("连接上了WIFI网络。。。。。");
}
//打电话
public void call() {
System.out.println("正在视频通话中。。。。。");
}
//发信息
public void sendMsg() {
System.out.println("正在发送文字+图片+音频的信息。。。。。");
}
}
package dh06.phone;
//测试类-手机
public class Test {
public static void main(String[] args) {
CommonPhone phone1 = new CommonPhone("诺基亚","TS");
phone1.sendMsg();
phone1.call();
phone1.play("慢慢喜欢你");
System.out.println("********************************");
SmartPhone phone2 = new SmartPhone("iPhone","14promax") ;
phone2.sendMsg();
phone2.call();
phone2.play("哪吒");
phone2.netWorkConn();
phone2.takePhotos();
}
}
package dh06.box;
//接口:代表生产墨盒的标准或约定
public interface InkBox {
//墨盒颜色
public String getColor();
}
package dh06.box;
//接口:代表生产纸张的标准或约定
public interface Paper {
//纸张大小
String getSize();
}
package dh06.box;
//彩色墨盒
public class ColorInkBox implements InkBox{
//墨盒颜色
public String getColor() {
return "彩色" ;
}
}
package dh06.box;
//彩色墨盒
public class WhiteBlackInkBox implements InkBox{
//墨盒颜色
public String getColor() {
return "黑白" ;
}
}
package dh06.box;
//生产A4纸张
public class A4Paper implements Paper {
public String getSize() {
return "A4纸" ;
}
}
package dh06.box;
//生产B5纸张
public class B5Paper implements Paper {
public String getSize() {
return "B5纸" ;
}
}
package dh06.box;
//打印机类
public class Printer {
//墨盒 纸张
InkBox inkBox;
Paper paper;
//安装墨盒
public InkBox getInkBox() {
return inkBox;
}
public void setInkBox(InkBox inkBox) {
this.inkBox = inkBox;
}
//往打印机中防纸
public Paper getPaper() {
return paper;
}
public void setPaper(Paper paper) {
this.paper = paper;
}
//使用墨盒在纸张上打印
public void print(InkBox inkBox,Paper paper) {
System.out.println("使用"+inkBox.getColor()+"的墨盒在"+paper.getSize()+"的纸张上打印。");
}
//--------------------------------------------------------------------------
}
package dh06.box;
//测试类
public class Test {
public static void main(String[] args) {
//声明墨盒和纸张对象
InkBox inkBox = null;
Paper paper = null;
// 创建打印机
Printer printer = new Printer();
// 彩色打印A4纸
inkBox = new ColorInkBox();
paper = new A4Paper();
//调用
printer.print(inkBox,paper);
// 黑白打印B5纸
inkBox = new WhiteBlackInkBox();
paper = new B5Paper();
//调用
printer.print(inkBox,paper);
}
}
package dh06.assemble;
//cpu接口
public interface CPU {
public String brand();//品牌
public String HZ();//主频
}
package dh06.assemble;
//内存接口
public interface EMS {
public String type();//类型
public String EMSLoad();//容量
}
package dh06.assemble;
//硬盘接口
public interface HardDisk {
public String HDLoad();//容量
}
package dh06.assemble;
public class AMDCPU implements CPU{
@Override
public String brand() {
return "AMD";
}
@Override
public String HZ() {
return "2.6HZ";
}
}
package dh06.assemble;
public class JSDEMS implements EMS {
@Override
public String type() {
return "金士顿";
}
@Override
public String EMSLoad() {
return "8G";
}
}
package dh06.assemble;
public class SSDHardDisk implements HardDisk {
@Override
public String HDLoad() {
return "1T SSD";
}
}
package dh06.assemble;
public class IntelCPU implements CPU{
@Override
public String brand() {
return "intel";
}
@Override
public String HZ() {
return "2.2HZ";
}
}
package dh06.assemble;
public class YCEMS implements EMS {
@Override
public String type() {
return "影驰";
}
@Override
public String EMSLoad() {
return "16G";
}
}
package dh06.assemble;
public class SataHardDisk implements HardDisk {
@Override
public String HDLoad() {
return "2T Sata";
}
}
package dh06.assemble;
//组装计算机
public class Computer {
//初始化参数
private CPU cpu = new AMDCPU();
private EMS ems = new JSDEMS();
private HardDisk hardDisk = new SSDHardDisk() ;
//-----------------------------------------------------
public CPU getCpu() {
return cpu;
}
public EMS getEms() {
return ems;
}
public HardDisk getHardDisk() {
return hardDisk;
}
public void setCpu(CPU cpu) {
this.cpu = cpu;
}
public void setEms(EMS ems) {
this.ems = ems;
}
public void setHardDisk(HardDisk hardDisk) {
this.hardDisk = hardDisk;
}
//-----------------------------------------------------
//组装
public void print() {
System.out.println("计算机已组装完成");
System.out.println("CPU品牌为:"+cpu.brand()+"\t主频为:"+cpu.HZ());
System.out.println("内存类型为:"+ems.type()+"\t容量为:"+ems.EMSLoad());
System.out.println("硬盘容量为:"+hardDisk.HDLoad());
}
}
package dh06.assemble;
//测试类
public class Test {
public static void main(String[] args) {
Computer computer1 = new Computer();
AMDCPU amdcpu = new AMDCPU();//AMDCPU
JSDEMS jsdems = new JSDEMS();//金士顿ems
SSDHardDisk ssdHardDisk = new SSDHardDisk();//ssd
computer1.setCpu(amdcpu);
computer1.setEms(jsdems);
computer1.setHardDisk(ssdHardDisk);
//打印
computer1.print();
System.out.println("**********************************");
//---------------------------------------------------
Computer computer2 = new Computer();
IntelCPU intelCPU = new IntelCPU();//IntelCPU
YCEMS ycems = new YCEMS();//影驰EMS
SataHardDisk sataHardDisk = new SataHardDisk();//Sata
computer2.setCpu(intelCPU);
computer2.setEms(ycems);
computer2.setHardDisk(sataHardDisk);
//打印
computer2.print();
}
}
package dh06.door;
//锁:开锁、上锁
public interface Lock {
public void lockUp();//上锁方法
public void openLock();//开锁方法
}
package dh06.door;
//门铃:拍照存储功能
public interface DoorBell {
public void takePictures();
}
package dh06.door;
//门--抽象类
public abstract class Door {
public abstract void open();//开
public abstract void close();//关
}
package dh06.door;
//防盗门:是门,具备锁的能力
public class TheftproofDoor extends Door implements Lock , DoorBell {
// 上锁
public void lockUp() {
System.out.println("插进钥匙,向左转三圈,锁上了,拔出钥匙。");
}
//开锁
public void openLock() {
System.out.println("插进钥匙,向右转三圈,锁开了,拔出钥匙。");
}
//开门
public void open() {
System.out.println("用力推,门打开了");
}
//关门
public void close() {
System.out.println("轻轻拉门,门关上了");
}
//按门铃拍照存储
public void takePictures() {
System.out.println("铃铃铃。。。。。咔嚓。。。拍照存储。");
}
}
package dh06.door;
//测试防盗门功能
public class Test {
public static void main(String[] args) {
TheftproofDoor door = new TheftproofDoor();
//关门上锁
door.close();
door.lockUp();
//拜访
door.takePictures();
//开锁开门
door.openLock();
door.open();
}
}
package dh06.test01;
/**
* Compute接口
* @author XXX
*
*/
public interface Compute {
public int computer(int n,int m);
}
package dh06.test01;
//计算类 +
public class CalAdd implements Compute{
@Override
public int computer(int n, int m) {
// TODO Auto-generated method stub
return (n + m);
}
}
package dh06.test01;
//计算类 -
public class CalSub implements Compute{
@Override
public int computer(int n, int m) {
// TODO Auto-generated method stub
return (n - m);
}
}
package dh06.test01;
//计算类 *
public class CalMul implements Compute{
@Override
public int computer(int n, int m) {
// TODO Auto-generated method stub
return (n * m);
}
}
package dh06.test01;
//计算类 /
public class CalDiv implements Compute {
@Override
public int computer(int n, int m) {
if (m == 0) {
System.out.println("分母为空!!!");
return 0;
} else {
return n / m;
}
}
}
package dh06.test01;
//UseCompute类
public class UseCompute {
public void useCom(Compute com, int one, int two) {
System.out.println(com.computer(one, two));
}
}
package dh06.test01;
// 测试类
public class Test {
public static void main(String[] args) {
UseCompute useCompute = new UseCompute();
useCompute.useCom(new CalAdd(), 5, 5);// +
useCompute.useCom(new CalSub(), 5, 5);// -
useCompute.useCom(new CalMul(), 5, 5);// *
useCompute.useCom(new CalDiv(), 5, 5);// /
}
}