知识卡片
接口按行为集合满足
接口声明所需方法;拥有相应方法集合的类型隐式满足它,不必在类型定义上显式登记。这样接口可表达调用者需要的行为,并由不受其控制的类型实现。空接口不要求任何方法,灵活但失去具体行为约束,后续需要运行时判断。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Go Language Foundations · 查看原书依据
Note that we don’t have to explicitly declare that they satisfy the interface: if a type possesses all of its methods, it can implicitly satisfy an interface. This is particularly useful when you want to design interfaces that are satisfied by types that you don’t own or control. type Circle struct { Radius float64 }
参考来源
- 位置:《Cloud Native Go》第03章《Go Language Foundations》(来源项目book-sources;源文件:extracted/Cloud Native Go (Matthew A. Titmus)/_epub-src/OEBPS/ch03.xhtml)
- 结论依据:接口声明所需方法;拥有相应方法集合的类型隐式满足它,不必在类型定义上显式登记。原文相应定义、示例或限制为此结论提供依据;补充纠错与版本边界在正文就近标明。
- 原始内容:Note that we don’t have to explicitly declare that they satisfy the interface: if a type possesses all of its methods, it can implicitly satisfy an interface. This is particularly useful when you want to design interfaces that are satisfied by types that you don’t own or control. type Circle struct { Radius float64 }