Clojure:使用代理和可变字段 [英] Clojure: Using proxy and mutable fields

查看:85
本文介绍了Clojure:使用代理和可变字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Clojure中使用代理来扩展一个Java类。我需要在超类中设置一个字段,我该怎么做呢?以下代码不起作用。

I'm using proxy in Clojure to extend a Java class. I need to set a field in the superclass, how can i do this? The code below doesn't work.

(proxy [BasicPlayer] []
  (open [url]
  (set! super/m_dataSource url)))


推荐答案

proxy


请注意,虽然方法fns可以
来覆盖受保护的方法,他们没有其他访问
给受保护的成员,也没有超级,因为这些能力不能
代理。

Note that while method fns can be provided to override protected methods, they have no other access to protected members, nor to super, as these capabilities cannot be proxied.

对不起,但它听起来你没有运气。你可以使用 proxy-super 调用受保护的超类方法,但我认为是这样。

Sorry, but it sounds like you're out of luck. You can call protected superclass methods with proxy-super, but I think that's about it.

运气 gen-class

(ns my.Player
  (:gen-class
   :extends BasicPlayer 
   :exposes {m_dataSource {:set -setDataSource}})

(defn -open [this url]
  (-setDataSource this url))

这篇关于Clojure:使用代理和可变字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆