2015-08-03 100 views
0

我有下面的代碼,儘管類和memeber方法是公共的,我不能在methodLMF中引用methodHF而不是methodBF。我試過以下內容:從外部類的內部類的引用方法

LMF.this.xxxx //but the methods do not show up 

請告訴我如何解決它。

代碼

class LMF { 
    LMF() {} 

    public methodLMF() { } // should return methodHF+methodBF 

    //class HF 
    class HF { 
     HF() {} 

     public methodHF(int x) {x++} 
    } 

    //class BF 
    class BF { 
     BF() {} 

     public methodBF(int x) {x++} 
    } 
} 
+0

請把實際可驗證的代碼。不只是骨架。 – Codebender

+1

這些是實例方法,所以如果你有一個*實例*的HF和一個*實例*的BF,你只能引用它們。 – RealSkeptic

回答

1

您需要創建HF和BF對象才能訪問此處的方法。

class LMF { 
    LMF() { 
    } 

    public int methodLMF(int x) { 
     return new HF().methodHF(x) + new BF().methodBF(x); 
    } // should return methodHF+methodBF 

    // class HF 
    class HF { 
     HF() { 
     } 

     public int methodHF(int x) { 
      return x++; 
     } 
    } 

    // class BF 
    class BF { 
     BF() { 
     } 

     public int methodBF(int x) { 
      return x++; 
     } 
    } 

    public static void main(String[] args) { 
     System.out.println(new LMF().methodLMF(1)); 
    } 
} 
0

您需要訪問它作爲

HF HF = this.new HF(); hF.methodHF()