1 package test_24_2; 2 3 public interface Target { 4 5 void method1(); 6 7 void method2(); 8 }
1 package test_24_2; 2 3 public class Adaptee { 4 5 public void method1() { 6 7 System.out.println("Adaptee method1()"); 8 } 9 }
1 package test_24_2; 2 3 public class Adapter implements Target { 4 5 private Adaptee adaptee; 6 7 public Adapter(Adaptee adaptee) { 8 this.adaptee = adaptee; 9 } 10 11 public void method1() { 12 this.adaptee.method1(); 13 } 14 15 public void method2() { 16 17 System.out.println("Adapter method2()"); 18 } 19 }
1 package test_24_2; 2 3 public class AdapterTest { 4 5 public static void main(String[] args) { 6 Adapter adapter = new Adapter(new Adaptee()); 7 8 adapter.method1(); 9 10 adapter.method2(); 11 } 12 }
结果如下:
Adaptee method1()
Adapter method2()