← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 6074.d.1.a.13 · 生成模型:未记录

默认实现复用共有逻辑

理解等级:中级

trait 方法可以附默认函数体。实现它的类型可以沿用默认,也可以覆盖。默认方法还可以调用同一 trait 中必须由各类型实现的方法,例如 summarize 调用 summarize_author,把变化点留给具体类型。覆写后不能直接调用同名方法的默认实现。

相关卡片 [[特征定义共享操作]]

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《The Rust Programming Language(第2版)》第10章 泛型类型、trait 与生命周期(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c10.xhtml`,块 0141) - 结论依据:原文以默认摘要复用必需的作者方法,说明复用与扩展点的组织方式。 - 原始内容:Default implementations can call other methods in the same trait, even if those other methods don’t have a default implementation. In this way, a trait can provide a lot of useful functionality and only require implementors to specify a small part of it.