gpt4 book ai didi

clojure - 代理和具体化有什么区别?

转载 作者:行者123 更新时间:2023-12-03 00:55:31 25 4
gpt4 key购买 nike

proxy 和 reify 有什么区别?我有一些示例代码:

(.listFiles (java.io.File. ".")
(proxy
[java.io.FileFilter] []
(accept [f]
(.isDirectory f))))

(.listFiles (java.io.File. ".")
(reify
java.io.FileFilter
(accept [this f]
(.isDirectory f))))

结果是一样的,使用proxy和reify哪个更好?

更新:

我发现了一些东西:

  • 代理不需要 this 作为第一个参数。
  • 代理支持父类(super class)。
  • 代理支持参数。

最佳答案

来自Clojure.org's overview of data types :

The method bodies of reify are lexical closures, and can refer to the surrounding local scope. reify differs from proxy in that:

  • Only protocols or interfaces are supported, no concrete superclass.
  • The method bodies are true methods of the resulting class, not external fns.
  • Invocation of methods on the instance is direct, not using map lookup.
  • No support for dynamic swapping of methods in the method map.

The result is better performance than proxy, both in construction and invocation. reify is preferable to proxy in all cases where its constraints are not prohibitive.

来源:http://clojure.org/datatypes

关于clojure - 代理和具体化有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11010558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com