读书人

最简略的适配器解释

发布时间: 2012-12-24 10:43:13 作者: rapoo

最简单的适配器解释
适配器就是起适配的作用:一个方法只接受A接口,如何让他接受B接口呢,很简单,写个适配器:

package test;public class AdapterTest {interface A{}interface B{}class C{void accept(A a){}}class Aadapter implements A{B b;public Aadapter(B b){this.b = b;}}public static void main(String[] args) {C c = new AdapterTest().new C();c.accept(new AdapterTest().new Aadapter(new B(){//B接口的匿名类实现}));}}

读书人网 >编程

热点推荐