0Pricing
Clojure Functional Programming & JVM Backend Development · 课时

用于多态的协议与多重方法

Clojure 提供两种灵活的多态工具,可与宏和模块化设计互相补充。本课程将教您使用协议和多重方法编写可扩展、低耦合的代码。

用于多态的协议与多重方法 是 CoddyKit 上的免费 Clojure Functional Programming & JVM Backend Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Clojure Functional Programming & JVM Backend Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Clojure Functional Programming & JVM Backend Development 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Polymorphism in Clojure

Clojure separates data from behavior. Protocols and multimethods let you dispatch behavior without classes or inheritance.

Defining a Protocol

A defprotocol names a set of method signatures, like an interface.

(defprotocol Shape
  (area [s])
  (perimeter [s]))

Implementing With a Record

Use defrecord to create a type that implements the protocol.

(defrecord Circle [r]
  Shape
  (area [_] (* Math/PI r r))
  (perimeter [_] (* 2 Math/PI r)))
(println (area (->Circle 2)))

Dispatch on Type

Protocols dispatch on the type of the first argument. This is fast and the most common polymorphism in Clojure.

Extending Existing Types

Use extend-protocol to add behavior to types you do not own, even built-in ones.

(extend-protocol Shape
  java.lang.Long
  (area [n] (* n n)))
(println (area 5))

When You Need More

Protocols only dispatch on type. When you must dispatch on a value, multiple arguments, or a computed key, reach for multimethods.

Defining a Multimethod

defmulti takes a dispatch function; defmethod supplies an implementation per dispatch value.

(defmulti describe :kind)
(defmethod describe :dog [_] "woof")
(defmethod describe :cat [_] "meow")
(println (describe {:kind :dog}))

The Default Method

Provide a :default method to handle unmatched dispatch values gracefully.

(defmethod describe :default [_] "unknown")
(println (describe {:kind :fish}))

Dispatch on Anything

The dispatch function can compute any value, even from multiple arguments, giving multimethods far more flexibility than protocols.

(defmulti combine (fn [a b] [(:type a) (:type b)]))
; dispatch on a pair of types

Choosing Between Them

Prefer protocols for type-based, performance-sensitive dispatch. Use multimethods when dispatch logic is richer than a single type.

Open for Extension

Both tools are open: new types or methods can be added later in separate namespaces without editing the original code, solving the expression problem cleanly.

Quick Check

Test your polymorphism knowledge.

Recap

You learned protocols with defrecord and extend-protocol for type dispatch, multimethods with defmulti/defmethod for value dispatch, default methods, and how both keep code open for extension.

常见问题解答

「用于多态的协议与多重方法」课时是免费的吗?

是的 — 「用于多态的协议与多重方法」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Clojure Functional Programming & JVM Backend Development 课程的其余内容,请升级到 CoddyKit PRO。 Clojure Functional Programming & JVM Backend Development 课程共包含 4 节课。

「用于多态的协议与多重方法」这节课中我会学到什么?

Clojure 提供两种灵活的多态工具,可与宏和模块化设计互相补充。本课程将教您使用协议和多重方法编写可扩展、低耦合的代码。 你通过在浏览器中直接运行的动手代码来练习 Clojure Functional Programming & JVM Backend Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Clojure Functional Programming & JVM Backend Development 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Clojure Functional Programming & JVM Backend Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「用于多态的协议与多重方法」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Clojure Functional Programming & JVM Backend Development 课中编写并运行代码吗?

能。每节 Clojure Functional Programming & JVM Backend Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 理解 Clojure 宏
  2. 定义与使用命名空间
  3. 使用模块组织代码
  4. 用于多态的协议与多重方法
← 返回 Clojure Functional Programming & JVM Backend Development