内部类习题9

创建一个至少含有一个方法的接口 在某个方法内定义一个内部类来实现这个接口  并返回对这个接口的引用  
 

// innerclasses/Ex9.java
// TIJ4 Chapter Innerclasses, Exercise 9, page 356
/* Create an interface with at least one method, and implement that
* interface by defining an inner class within a method, which returns a
* reference to your interface.
*/

interface Ex9Interface {
 void say(String s);
}

public class Ex9 {
 Ex9Interface f() {
  class Inner implements Ex9Interface {
   public void say(String s) {
    System.out.println(s);
   }
  }
  return new Inner();
 }
 public static void main(String[] args) {
  Ex9 x = new Ex9();
  x.f().say("hi");
 }
}

 

你可能感兴趣的:(interface,string,reference,class)