实现在内部类中声明的接口

时间:2012-02-04 09:35:35

标签: java interface inner-classes

这是一个Java问题 如何在外部类中实现内部类的接口?

我尝试以下方法,但徒劳无功。谢谢

class A implements interface B.C{
   static class B{
        interface C{

        }
   }
}

1 个答案:

答案 0 :(得分:1)

我会这样做:
假设两个类都在相同的包中并且具有适当的导入。

public class Nestedinterface {
     public interface NestI{
         void show();
     }
}

public class NestedinterfaceImpl implements NestI {
    public static void main(String a[]) {
        NestI n = new NestedinterfaceImpl();
        n.show();
    }
    public void show() {
        // TODO Auto-generated method stub
        System.out.println("Hello world");    
    }
}