适配器模式1

xiaoxiao2021-02-27  224

/** * 适配器模式 -- 类的适配器模式 * 当希望将一个类(Source)转换为满足一个接口(Targetable)的新的类(Adapter)时, * 新的类继承待适配的类并实现接口 */ public class Adapter extends Source implements Targetable { @Override public void method02() { System.out.println("This is the 'method02' method in Adapter implements Targerable."); } }

public class Source { public void method01(){ System.out.println("This is the 'method01' method in Source."); } } public interface Targetable { void method01(); void method02(); }

public class AdapterTest { public static void main(String[] args) { Adapter adapter = new Adapter(); adapter.method01(); adapter.method02(); } }

转载请注明原文地址: https://www.6miu.com/read-9072.html

最新回复(0)