Clojure编程乐趣:多态和表达式

(defprotocol Connectable
  (my-cat [this other]))
=> Connectable
#_ 扩展 List 类型
(extend-type java.util.List
  Connectable
  (my-cat [this other]
    (println "List my-cat")
    (concat this other)))
=> nil
(my-cat [1 2 3] [4 5 6])
List my-cat
=> (1 2 3 4 5 6)
#_ 扩展 String 类型
(extend-type String
  Connectable
  (my-cat [this other]
    (println "String my-cat")
    (.concat this other)))
=> nil
(my-cat "1231" "213123")
String my-cat
=> "1231213123"

表达式问题

指在不修改已定义代码的前提下,为既有具体类实现一套既有的抽象方法。

Clojure 的协议可以扩展到任何有意义的类型上。甚至是类型实现者或者协议实现者都没想过的地方。

引自《Clojure编程乐趣 第二版》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangningboo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值