public class Doubler {
   public static int doubleMe(Holder h){
      return h.getAmount()*2;
   }
   
   class Holder{
      int amount = 10;
      public void doubleAmount(){
         amount = Doubler.doubleMe(this);
      }
      public int getAmount(){
         return amount;
      }
   }
}

解决方案 »

  1.   


    public class Test {
    public static void main(String[] args) {
    Holder h = new Holder();
    System.out.println(Doubler.doubleMe(h));
    }
    }class Doubler {
    public static int doubleMe(Holder h) {
    return h.getAmount() * 2;
    }
    }class Holder {
    int amount = 10; public void doubleAmount() {
    amount = Doubler.doubleMe(this);
    } public int getAmount() {
    return amount;
    }
    }不知道,这样是不是楼主的意思!
      

  2.   


    public class Doubler {
       public static int doubleMe(Holder h){
          return h.getAmount()*2;
       }
       
       class Holder{
          int amount = 10;
          public void doubleAmount(){
             amount = this.getAmount()*2;
          }
          public int getAmount(){
             return amount;
          }
       }
    }
      

  3.   

    或者你Holder的doubleAmount方法根本就是多余的。
      

  4.   

    嗯,把内部类分出来就可以了。
    amount对象的doubleAmount()调用Doubler类的静态方法
      

  5.   


    class Doubler {
    public static int doubleMe(IHolder h) {
    return h.getAmount() * 2;
    }
    }interface IHolder{
    int getAmount();
    }class Holder implements IHolder{
    int amount = 10; public void doubleAmount() {
    amount = Doubler.doubleMe(this);
    } public int getAmount() {
    return amount;
    }
    }
      

  6.   

    赞同。补充一点,一般会把Doubler和IHolder放在一个包里(A包),Holder放在另一个包里(B包)。这种情况下,依赖关系是B包依赖(使用)A包。当需要改变Holder时,你甚至可以不知道有Holder和B包的存在,只需根据IHolder接口直接提供你的实现即可。此外,当你可能有两种实现IHolder的方案,并且在程序执行的时候要视情况交替使用这两种方案。此时,可以按两种方案分别实现IHolder,在程序运行时根据情况将它们的实例传入Doubler.doubleMe(IHolder h)即可。从这里可以看出,Doubler并不像原来那样依赖于Holder类,当Holder发生改变时,Doubler不会被迫改变。