你定义了这两个构造器
  public PegAdapter(RoundPeg peg){this.roundPeg=peg;}
 
  public   PegAdapter(SquarePeg peg){this.squarePeg=peg;}
但是并没有定义
public PegAdapter(){};这样一个构造器啊,
所以你在main方法中定义
PegAdapter a=new PegAdapter();
应该会出错吧?

解决方案 »

  1.   

    错了,public PegAdapter(){}后面没有分号。
      

  2.   


     加上这话还有错误观点 public PegAdapter(){}
    public class PegAdapter implements IRoundPeg,ISquarePeg{错误提示在上面这一行
      

  3.   

    声明实现IRoundPeg接口,却没有实现对应的方法?
      

  4.   

    做如下调整,仍然抱错
    interface IRoundPeg {
    void insertIntoHole(String msg);}interface ISquarePeg {
    void insert(String str);}class SquarePeg implements ISquarePeg {
    public void insert(String str) {
    System.out.println("SquarePeg insert():" + str);
    }}class RoundPeg implements IRoundPeg {
    public void insertIntoHole(String msg) {
    System.out.println("RoundPeg insertIntoHole():" + msg);
    }
    }public class PegAdapter implements IRoundPeg, ISquarePeg { RoundPeg roundPeg;
    SquarePeg squarePeg; public PegAdapter() {
    } public PegAdapter(RoundPeg peg) {
    this.roundPeg = peg;
    }
    public PegAdapter(SquarePeg peg) {
    this.squarePeg = peg;
    } public void sinsert(String str) {
    squarePeg.insert(str);
    }
    public void rinsert(String str) {
    roundPeg.insertIntoHole(str);
    } public static void main(String args[]) {
    PegAdapter a = new PegAdapter(); a.sinsert("s"); a.rinsert("r"); }
    }
      

  5.   

    这样就好了,不知道你在做什么interface IRoundPeg {
      void insertIntoHole(String msg);}interface ISquarePeg {
      void insert(String str);}class SquarePeg
        implements ISquarePeg {
      public void insert(String str) {
        System.out.println("SquarePeg insert():" + str);
      }}class RoundPeg
        implements IRoundPeg {
      public void insertIntoHole(String msg) {
        System.out.println("RoundPeg insertIntoHole():" + msg);
      }
    }public class PegAdapter {  RoundPeg roundPeg = new RoundPeg();
      SquarePeg squarePeg = new SquarePeg();  public PegAdapter() {
      }  public PegAdapter(RoundPeg peg) {
        this.roundPeg = peg;
      }  public PegAdapter(SquarePeg peg) {
        this.squarePeg = peg;
      }  public void sinsert(String str) {
        squarePeg.insert(str);
      }  public void rinsert(String str) {
        roundPeg.insertIntoHole(str);
      }  public static void main(String args[]) {
        PegAdapter a = new PegAdapter();    a.sinsert("s");    a.rinsert("r");  }
    }
      

  6.   

    public  class Adapter {  RoundPeg roundPeg ;
      SquarePeg squarePeg;  Adapter() {
       roundPeg=new RoundPeg();
        squarePeg=new SquarePeg();
      }   Adapter(RoundPeg peg) {
        this.roundPeg = peg;
      }   Adapter(SquarePeg peg) {
        this.squarePeg = peg;
      }  public void sinsert(String str) {
        squarePeg.insert(str);
      }  public void rinsert(String str) {
        roundPeg.insertIntoHole(str);
      }  public static void main(String args[]) {
        Adapter a = new Adapter();    a.sinsert("s");    a.rinsert("r");  }
    }
    这样是可以拉,可这个构造涵数起什么作用怎么没用到??
    源代码如下:
    http://www.jdon.com/designpatterns/adapter.htm