40 lines
1.9 KiB
Plaintext
40 lines
1.9 KiB
Plaintext
// 方法覆盖遵守参数可更宽、返回可更具体的规则。
|
|
// 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)
|
|
button = Button("扩大矩形")
|
|
button.onClick(actions.widen)
|
|
root = Column(24, 16, TextBlock("参数可以更宽,返回类型可以更具体", 2), rect, button, ui.textOf(count, "value"))
|
|
page = Page()
|
|
page.title = "方法覆写契约"
|
|
page.render(root)
|