docs: 逐模块细化中文逻辑注释并覆盖测试与示例
This commit is contained in:
@@ -1,23 +1,33 @@
|
||||
// 方法覆盖遵守参数可更宽、返回可更具体的规则。
|
||||
// WideRect 复用基类原生测量后返回 Sized 子类结果;事件改变基础 width 后按同一路径重测。
|
||||
// MeasureResult 的名义子类,不增加字段,用于演示返回值协变。
|
||||
class Sized: MeasureResult {}
|
||||
// 用源方法显式委托原生测量,保留 Mount/Constraints 的基类契约。
|
||||
class BaseRect: RectBlock {
|
||||
// 原生方法按给定出现位置与约束测量,不把几何存进共享对象。
|
||||
function measure(self: BaseRect, mount: Mount, constraints: Constraints): MeasureResult {
|
||||
return RectBlock.measure(self, mount, constraints)
|
||||
}
|
||||
}
|
||||
// 覆盖参数放宽为 Object,返回缩窄为 Sized;不能反过来缩窄参数或放宽返回。
|
||||
class WideRect: BaseRect {
|
||||
// 调用基类后复制测量数值到新的 Sized;新结果仍满足 MeasureResult 协议。
|
||||
function measure(self: WideRect, mount: Object, constraints: Object): Sized {
|
||||
measured = BaseRect.measure(self, mount, constraints)
|
||||
return Sized(measured.width, measured.height)
|
||||
}
|
||||
}
|
||||
// 保存具体 WideRect 和计数器,使宽度修改和点击次数同时作为业务变化传播。
|
||||
class Actions {
|
||||
rect: WideRect
|
||||
count: Object
|
||||
// 先改 width 再递增计数,新的布局刷新会使用上述动态测量覆盖。
|
||||
function widen(self: Actions): Unit {
|
||||
self.rect.width = 260
|
||||
self.count.increment()
|
||||
}
|
||||
}
|
||||
// 初始矩形宽 180;按钮事件完成后应变为 260,同时文字显示计数。
|
||||
rect = WideRect(180, 60, 8, "#ED6A5A")
|
||||
count = ui.state(0)
|
||||
actions = Actions(rect, count)
|
||||
|
||||
Reference in New Issue
Block a user