怎么同时继承父类又实现特定接口

24 views
Skip to first unread message

Tienson Qin

unread,
May 20, 2014, 10:07:03 AM5/20/14
to cn-cl...@googlegroups.com
clojure同时继承一个java父类,又可以实现一个特定接口一般怎么处理,比如我要定义一个netty channelhandler,既要继承一个adapter又要实现shareable接口,reify只支持接口,proxy貌似不支持同时处理,reify包裹proxy可以吗,手机发的问题,后面会附上代码

Shouxun Yang

unread,
May 20, 2014, 10:28:46 AM5/20/14
to cn-cl...@googlegroups.com

<<Clojure编程>>一书第18章有一个流程图,告诉你该如何选择。简单说,你可以选 defrecord.

On May 20, 2014 10:07 PM, "Tienson Qin" <tiens...@gmail.com> wrote:
clojure同时继承一个java父类,又可以实现一个特定接口一般怎么处理,比如我要定义一个netty channelhandler,既要继承一个adapter又要实现shareable接口,reify只支持接口,proxy貌似不支持同时处理,reify包裹proxy可以吗,手机发的问题,后面会附上代码

--
中文社区博客:http://blog.clojure.cn/
中文问答网站:http://ask.clojure.cn/
中文邮件列表:https://groups.google.com/d/forum/cn-clojure?hl=zh-CN
---
您收到此邮件是因为您订阅了 Google 网上论坛的“CN-Clojure”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到cn-clojure+...@googlegroups.com
要向此网上论坛发帖,请发送电子邮件至 cn-cl...@googlegroups.com
通过以下网址访问此论坛:http://groups.google.com/group/cn-clojure
要查看更多选项,请访问 https://groups.google.com/d/optout

dennis zhuang

unread,
May 20, 2014, 2:20:42 PM5/20/14
to cn-cl...@googlegroups.com
proxy可以啊,比如:

(defmacro def-threadlocal-var
  "A macro to define thread-local var.
    It also implement clojure.lang.IDeref interface,
   so you can get it's value by @ or deref."
  [name]
  (let [name (with-meta name {:tag ThreadLocal})]
    `(def ~name
       (proxy [ThreadLocal clojure.lang.IDeref] []
         (initialValue [] true)
         (deref [] (.get ~(with-meta 'this {:tag `ThreadLocal})))))))

我定义的一个宏,用来生成thread-local变量,proxy同时继承ThreadLocal并实现initialValue方法,并实现接口clojure.lang.IDeref的deref方法。




您收到此邮件是因为您订阅了Google网上论坛中的“CN-Clojure”论坛。
要退订此论坛并停止接收此论坛的电子邮件,请发送电子邮件到cn-clojure+...@googlegroups.com
要发帖到此论坛,请发送电子邮件至cn-cl...@googlegroups.com
通过http://groups.google.com/group/cn-clojure访问此论坛。
要查看更多选项,请访问https://groups.google.com/d/optout



--
庄晓丹
Email:        killm...@gmail.com xzh...@avos.com
Site:           http://fnil.net
Twitter:      @killme2008


Tienson Qin

unread,
May 20, 2014, 9:45:46 PM5/20/14
to cn-cl...@googlegroups.com
刚才测试过了,proxy是可以,谢谢dennis,读了很多您的博客,受益匪浅


您收到此邮件是因为您订阅了Google网上论坛“CN-Clojure”中的主题。
要退订此主题,请访问https://groups.google.com/d/topic/cn-clojure/fQCy_mxs1zk/unsubscribe
要退订此论坛及其所有主题,请发送电子邮件到cn-clojure+...@googlegroups.com

Tienson Qin

unread,
May 20, 2014, 9:54:15 PM5/20/14
to cn-cl...@googlegroups.com
defrecord只支持接口,我用类的的话,就会报错:
CompilerException java.lang.IllegalArgumentException: only interfaces are supported, had: io.netty.channel.ChannelInboundHandlerAdapter


Reply all
Reply to author
Forward
0 new messages