1. defmulti java
宏defmulti
和defmethod
常常被用在一块儿来定义 multimethod. 宏defmulti
的参数包括一个方法名以及一个dispatch函数,这个dispatch函数的返回值会被用来选择到底调用哪一个重载的函数。宏defmethod
的参数则包括方法名,dispatch的值, 参数列表以及方法体。一个特殊的dispatch值:default
是用来表示默认状况的 — 即若是其它的dispatch值都不匹配的话,那么就调用这个方法。defmethod
多定义的名字同样的方法,它们的参数个数必须同样。传给multimethod的参数会传给dipatch函数的。实现相似java的重载函数
示例:spa
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)