2012-01-13 12 views
7

Aşağıdaki sınıf hiyerarşi senaryosuna sahibim; A Sınıfı bir yönteme sahiptir ve B Sınıfı, yerel olarak iç içe geçmiş bir sınıftan süper sınıftan bir yöntem aramak istediğim Sınıf A'yı genişletir. Umarım iskelet yapısı, senaryoyu daha net bir şekilde görüntüleyebilir Java böyle çağrılara izin veriyor mu?Java Yerel Yerleşik Sınıflar ve süper yöntemlere erişme

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

Dış sınıfın geçersiz kılınan yöntem uygulamalarını bir iç sınıftan çağırmak istediğinizden emin misiniz? Bana doğru karışıklık gibi görünüyor. –

+0

@Tom Hawtin - Bunun bir "iç" sınıf değil, bir "yerel anonim" sınıfı olduğuna inanıyoruz - ki bu daha çok bir karmaşa yapıyor. – emory

+0

@emory Teknik olarak, anonim iç sınıflar yerel sınıflar iç sınıflardır. –

cevap

21

sadece call() yılında A.getCount() aramaya B.super.getCount() kullanabilirsiniz.

5

Sen belki

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

çizgisinde B.super.getCount()

4

şey kullanmak ettik?

İlgili konular