2012-01-13 70 views
7

我有以下類層次結構方案;類A有一個方法,而類B擴展了類A,我想從本地嵌套類的超類中調用一個方法。 我希望骨架結構更清晰地描述場景 Java是否允許這樣的調用?Java本地嵌套類和訪問超級方法

class A{ 
    public Integer getCount(){...} 
    public Integer otherMethod(){....} 
} 

class B extends A{ 
    public Integer getCount(){ 
    Callable<Integer> call = new Callable<Integer>(){ 
     @Override 
     public Integer call() throws Exception { 
     //Can I call the A.getCount() from here?? 
     // I can access B.this.otherMethod() or B.this.getCount() 
     // but how do I call A.this.super.getCount()?? 
     return ??; 
     } 
    } 
    ..... 
    } 
    public void otherMethod(){ 
    } 
} 
+1

您真的確定要調用內部類的外部類的重寫方法實現嗎?看起來像一個正確的混亂給我。 – 2012-01-13 17:02:56

+0

@湯姆霍金 - 我認爲這是一個「本地匿名」類而不是「內部」類 - 這使得它更加混亂。 – emory 2012-01-13 17:34:28

+0

@emory從技術上講,匿名內部類是本地類是內部類。 – 2012-01-13 17:38:23

回答

21

你可以使用B.super.getCount()A.getCount()call()

5

您對使用B.super.getCount()

4

沿

package com.mycompany.abc.def; 

import java.util.concurrent.Callable; 

class A{ 
    public Integer getCount() throws Exception { return 4; } 
    public Integer otherMethod() { return 3; } 
} 

class B extends A{ 
    public Integer getCount() throws Exception { 
     Callable<Integer> call = new Callable<Integer>(){ 
      @Override 
      public Integer call() throws Exception { 
        //Can I call the A.getCount() from here?? 
        // I can access B.this.otherMethod() or B.this.getCount() 
        // but how do I call A.this.super.getCount()?? 
        return B.super.getCount(); 
      } 
     }; 
     return call.call(); 
    } 
    public Integer otherMethod() { 
     return 4; 
    } 
} 

也許是東西線?