//用户的目标接口 public interface Duck { void quack(); } //被适配者 public class Chicken { public void crow(){ System.out.println("公鸡打鸣!"); } } //适配器 public class ChickenAdapter implements Duck{ Chicken chicken; public ChickenAdapter (Chicken chicken){ this.chicken = chicken; } @Override public void quack() { chicken.crow(); } } //客户端 public class Client { //通过鸭子接口让鸡打鸣 public static void main(String[] args){ Duck chickenAdapter = new ChickenAdapter(new Chicken()); chickenAdapter.quack(); } }
原文:https://www.cnblogs.com/camcay/p/12358836.html