现在的位置: 首页 > 综合 > 正文

java在内部类中实现接口

2013年05月15日 ⁄ 综合 ⁄ 共 469字 ⁄ 字号 评论关闭

interface OutInterface { // 定义一个接口
 public void f();
}

public class InterfaceInner { // 主类
 public static void main(String args[]) {
  OuterClass2 out = new OuterClass2();
  OutInterface outinter = out.doit();
  outinter.f();
 }
}

class OuterClass2 {
 // 定义一个内部类,并且接口OutInterface
 private class InnerClass implements OutInterface {
  InnerClass(String s) {
   System.out.println(s);
  }

  public void f() {
   System.out.println("访问内部类中的f()方法");
  }
 }

 public OutInterface doit() { // 方法返回接口
  return new InnerClass("访问内部类构造方法");
 }
}

抱歉!评论已关闭.