2012-01-13 8 views
7

Tengo el siguiente escenario de jerarquía de clases; La clase A tiene un método y la clase B amplía la clase A, donde deseo llamar a un método de la clase superior de una clase anidada localmente. Espero que una estructura esquelética represente el escenario más claramente ¿Permite Java tales llamadas?Clases anidadas locales de Java y acceso a los súper métodos

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

¿De verdad está seguro de que quiere llamar a implementaciones de método anuladas de la clase externa de una clase interna? Me parece un desastre. –

+0

@Tom Hawtin: creo que esta es una clase "anónima local" y no una clase "interna", lo que la hace mucho más desordenada. – emory

+0

@emory Técnicamente, las clases internas anónimas son clases locales que son clases internas. –

Respuesta

21

sólo puede utilizar B.super.getCount() llamar A.getCount() en call().

5

Tienes que utilizar B.super.getCount()

4

Algo a lo largo de las líneas de

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

tal vez?

Cuestiones relacionadas