← 返回刚才阅读的位置

知识卡片

结构图卡 · 4545.g.8.al.3 · 生成模型:GPT-6

小接口让检查只依赖所需行为

理解等级:中级

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 处

    参考来源

    - 位置:《通过测试学习 Go 编程》结构体,方法和接口,spine009(book-sources;extracted/通过测试学习 Go 编程/_epub-src/OEBPS/Text/08.html)。 - 结论依据:真实Shape定义、三实现及统一checkArea说明解耦。 - 原始内容:type Shape interface { Area() float64 }