栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

模拟Java中的鸭子输入

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

模拟Java中的鸭子输入

如果知道要公开的一组API,例如说您想访问length方法和迭代器方法,则可以定义一个接口:

public interface TheInterfaceIWant {  int length();  void quack();}
如果您希望能够使用此接口来访问未实现此接口的实例上的相应方法,则可以使用代理类:http
//download.oracle.com/javase/1.4.2/docs/api/java
/lang/reflect/Proxy.html

所以你创建一个代理

final Object aDuck = ...;TheInterfaceIWant aDuckWrapper = (TheInterfaceIWant) Proxy.newProxyInstance(    TheInterfaceIWant.class.getClassLoader(),    new Class[] { TheInterfaceIWant.class },    new InvocationHandler() {      public Object invoke(          Object proxy, Method method, Object[] args)          throws Throwable {        return aDuck.getClass().getMethod( method.getName(), method.getParameterTypes()).invoke(aDuck, args);      }    });

然后,您可以像使用动态类型语言的鸭子一样使用包装器。

if (aDuckWrapper.length() > 0) {  aDuckWrapper.quack();}

这是一个全长可运行示例,该示例使用包装程序将“ Quack”打印四次:

import java.lang.reflect.*;public class Duck {  // The interface we use to access the duck typed object.  public interface TheInterfaceIWant {    int length();    void quack();  }  // The underlying instance that does not implement TheInterfaceIWant!  static final class Foo {    public int length() { return 4; }    public void quack() { System.out.println("Quack"); }  }  public static void main(String[] args) throws Exception {    // Create an instance but cast away all useful type info.    final Object aDuck = new Foo();    TheInterfaceIWant aDuckWrapper = (TheInterfaceIWant) Proxy.newProxyInstance(        TheInterfaceIWant.class.getClassLoader(),        new Class[] { TheInterfaceIWant.class },        new InvocationHandler() {          public Object invoke(   Object proxy, Method method, Object[] args)   throws Throwable { return aDuck.getClass().getMethod(     method.getName(), method.getParameterTypes()).invoke(aDuck, args);          }        });    for (int n = aDuckWrapper.length(); --n >= 0;) {      // Calling aDuck.quack() here would be invalid since its an Object.      aDuckWrapper.quack();    }  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/508814.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号