1. defmulti  

  宏defmulti 和defmethod 经常被用在一起来定义 multimethod. 宏defmulti 的参数包括一个方法名以及一个dispatch函数,这个dispatch函数的返回值会被用来选择到底调用哪个重载的函数。宏defmethod 的参数则包括方法名,dispatch的值, 参数列表以及方法体。一个特殊的dispatch值:default 是用来表示默认情况的 — 即如果其它的dispatch值都不匹配的话,那么就调用这个方法。defmethod 多定义的名字一样的方法,它们的参数个数必须一样。传给multimethod的参数会传给dipatch函数的。实现类似java的重载

示例:

1 (defmulti what_am_i class)
2 (defmethod what_am_i Number [args] (println args "is num"))
3 (defmethod what_am_i String [args] (println args "is String"))
4 (defmethod what_am_i :default [args] (println args "is default"))
5 (what_am_i 19)
6 (what_am_i "luochao")
7 (what_am_i true)

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-22
  • 2021-12-15
  • 2021-06-16
  • 2021-07-04
  • 2021-10-16
猜你喜欢
  • 2021-10-19
  • 2021-06-03
  • 2021-07-15
  • 2022-03-02
  • 2021-06-01
  • 2022-01-28
  • 2022-12-23
相关资源
相似解决方案