先点击看看题目要求:uml 类图

适配器代码实现

class Address{
  public void street(){System.out.println("正常街道");}
  public void zip(){System.out.println("正常邮编");}
  public void city(){System.out.println("正常地方");}
}

class DutchAddress{
  public void straat(){System.out.println("荷兰语街道");}
  public void postcode(){System.out.println("荷兰语邮编");}
  public void plaats(){System.out.println("荷兰语地方");}
}

class DutchAddressAdapter extends DutchAddress{
  private Address address;
  public DutchAddressAdapter(Address addr){
    this.address=addr;
  }
  public void straat(){
    this.address.street();
  }
  public void postcode(){
    this.address.zip();
  }
  public void plaats(){
    this.address.city();
  }
}

public class Test {
  public static void main(String[] args){
    Address addr=new Address();
    DutchAddress addrAdapter=new DutchAddressAdapter(addr);
    System.out.println("\n THE DUCTH ADDRESS\n");
    testDutch(addrAdapter);
  }
  static void testDutch(DutchAddress addr){
    addr.straat();
    addr.postcode();
    addr.plaats();
  }
}

附赠一个画圆和画方的

class Yuan{
    public void pp(String str){
        System.out.println("我只能pp画圆");
        System.out.println("圆打出来个 "+str);    
    }
}

class Fang{
    public void ppp(String str){
        System.out.println("我只能ppp画方");
        System.out.println("方打出来个 "+str);    
    }
}
class fangAdapter extends Yuan{
    
    private Fang fang;
    public fangAdapter (Fang fang){
        System.out.println("我能用pp画圆,也能用ppp画方");
        this.fang=fang;
    }
    public void ppp(String str){
        this.fang.ppp(str);
    }
}

public class RoundSquere {

    public static void main(String[] args){
        
        Fang f1=new Fang();
        
        fangAdapter fy=new fangAdapter(f1);
        
        fy.pp("hhh");
        fy.ppp("ooo");
        
    }
}

牙小木木
1.5k 声望80 粉丝

iamtb.cn