// 字段类型契约示例:CounterText 同时有原生文本载荷和源 Number 字段。 // 绑定显示读取该字段,GUI 回调与 Console 赋值受同一契约约束。 // 增加 value:Number;构造时先传原生 text,再传源字段值。 class CounterText: TextBlock { value: Number // 收到 click 后把已有数值改为 10,依赖文字随之更新,不修改原生 text 内容。 function change(self: CounterText, event: Event): Unit { self.value = 10 } } // 声明 target 必须是 Block,可接收 CounterText,因为它是 TextBlock 的子类。 class Model { target: Block } // 构造唯一受检对象,Model、事件回调和 textOf 均引用它。 counter = CounterText("共享的原生子类对象", 9) model = Model(counter) button = Button("把 Number 字段改成 10") connection = button.on("click", counter.change) page = Page() page.title = "字段类型契约" page.render(Column(24, 16, counter, ui.textOf(counter, "value"), button, TextBlock("GUI 和源码写入同一个受检查的字段")))