2012-01-13 23 views
7

Ho uno scenario di gerarchia di classe successivo; La Classe A ha un metodo e la Classe B estende la Classe A, dove voglio chiamare un metodo da super classe da una classe nidificata localmente. Spero che una struttura scheletrica mostri lo scenario in modo più chiaro. Java consente tali chiamate?Classi nidificate locali Java e accesso a super metodi

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

Sei davvero sicuro di voler chiamare le implementazioni del metodo sottoposto a override della classe esterna da una classe interna? Sembra un vero casino per me. –

+0

@Tom Hawtin - Credo che questa sia propriamente una classe "locale anonima" e non una classe "interiore" - il che lo rende molto più disordinato. – emory

+0

@emory Tecnicamente, le classi interne anonime sono classi locali sono classi interne. –

risposta

21

Si può semplicemente utilizzare B.super.getCount() chiamare A.getCount() in call().

5

Hai per usare B.super.getCount()

4

Qualcosa sulla falsariga di

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; 
    } 
} 

forse?