知识卡片
小接口让检查只依赖所需行为
Shape只要求Area() float64,checkArea因此可处理矩形、圆和三角形而不读取它们的具体字段。满足方法集合即隐式实现,无需显式implements;接口保证可调用的行为签名,不保证面积公式、单位或输入有效性。新类型能加入已有检查流程,而不是继续为每种形状复制断言。
结构图:
类图
classDiagram
class Shape {
<<interface>>
+Area() float64
}
class Rectangle {
+Width float64
+Height float64
+Area() float64
}
class Circle {
+Radius float64
+Area() float64
}
class Triangle {
+Base float64
+Height float64
+Area() float64
}
Shape <|.. Rectangle : 隐式实现
Shape <|.. Circle : 隐式实现
Shape <|.. Triangle : 隐式实现
类型与方法来自本章真实代码;虚线表示按方法集合隐式实现,不表示继承。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 结构体,方法和接口 · 查看原书依据
type Shape interface { Area() float64 }
参考来源
- 位置:《通过测试学习 Go 编程》结构体,方法和接口,spine009(book-sources;extracted/通过测试学习 Go 编程/_epub-src/OEBPS/Text/08.html)。
- 结论依据:真实Shape定义、三实现及统一checkArea说明解耦。
- 原始内容:type Shape interface { Area() float64 }