标签:this 需要 new t void ble interface gen ted es2017
1.需要适配的两个接口
public interface Duck { public void quack(); public void fly(); } public interface Turkey { public void gobble(); public void fly(); }
2.适配器转换接口
//这里是披着鸭皮的火鸡
public class TurkeyAdapter implements Duck { private Turkey turkey; public TurkeyAdapter(Turkey turkey){ this.turkey=turkey; } public void quack() { // TODO Auto-generated method stub this.turkey.gobble(); } public void fly() { // TODO Auto-generated method stub turkey.fly(); } }
3.具体实现需要适配的接口的类
public class MallardDuck implements Duck{ public void quack() { System.out.println("MallardDuck quack"); } public void fly() { System.out.println("MallardDuck fly"); } } public class WildTuekey implements Turkey{ public void gobble() { // TODO Auto-generated method stub System.out.println("WildTuekey gobble"); } public void fly() { // TODO Auto-generated method stub System.out.println("WildTuekey fly"); } }
4.应用
public class App { public static void main(String[] args) { MallardDuck duck=new MallardDuck(); WildTuekey turkey=new WildTuekey(); TurkeyAdapter adapter=new TurkeyAdapter(turkey); duck.fly(); duck.quack(); adapter.fly(); adapter.quack(); turkey.fly(); turkey.gobble(); } }
5.接口应该是这样的,鸭接口调出来 的是火鸡的方法
标签:this 需要 new t void ble interface gen ted es2017
原文地址:http://www.cnblogs.com/Niel-3/p/7467344.html