标签:11.4 void dog Dog Cat cat public
实验8:适配器模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解适配器模式的动机,掌握该模式的结构;
2、能够利用适配器模式解决实际问题。
[实验任务一]:双向适配器
实现一个双向适配器,使得猫可以学狗叫,狗可以学猫抓老鼠。
实验要求:
1. 画出对应的类图;
2. 提交源代码;
Adapter.java
package test8;
public class Adapter implements Cat,Dog{
private Cat cat=new Realcat();
private Dog dog=new Realdog();
public void setcat(Cat _cat){
cat=_cat;
}
public void setdog(Dog _dog){
dog=_dog;
}
public void zhua(){}
public void wang(){}
public void run(){
System.out.println("小狗学小猫:");
cat.zhua();
}
public void cry(){
System.out.println("小猫学小狗:");
dog.wang();
}
}
Cat.java
package test8;
public interface Cat {
public void cry();
public void zhua();
}
Dog.java
package test8;
public interface Dog {
public void wang();
public void run();
}
Realcat.java
package test8;
public class Realcat implements Cat{
public void cry(){
System.out.println("喵喵叫!");
}
public void zhua(){
System.out.println("抓老鼠!");
}
}
Realdog.java
package test8;
public class Realdog implements Dog{
public void wang(){
System.out.println("汪汪叫!");
}
public void run(){
System.out.println("快快跑!");
}
}
main.java
package test8;
public class main {
public static void main(String[] args) {
Dog dog=new Adapter();
Cat cat=new Adapter();
dog.run();
cat.cry();
}
}
3.注意编程规范。
标签:11.4,
void,
dog,
Dog,
Cat,
cat,
public
From: https://www.cnblogs.com/0608lin/p/18525502