52 lines
2.9 KiB
Plaintext
52 lines
2.9 KiB
Plaintext
// 源类继承原生视觉类型:原生载荷负责基础绘制,新增字段和覆盖方法由源类提供。
|
|
// label 在树中出现多次但仍是同一对象,原生方法和源字段写入共同更新各出现位置。
|
|
// 扩展 TextBlock 的业务字段;其文本/颜色仍由原生父类存储与绘制。
|
|
class StatusText: TextBlock {
|
|
// 构造后保存颜色策略;CustomStatus 继续继承此字段,源覆盖中通过同一 self 读取。
|
|
activeColor: String
|
|
|
|
// 用继承的 setText/setColor 改原对象;activeColor 是构造时追加的源字段。
|
|
function activate(self: StatusText, event: Event): Unit {
|
|
// 这是基类事件策略,本例实际 label 是 CustomStatus,因此 ActionButton 转发会选中下方覆盖。
|
|
self.setText("继承的原生文本已更新")
|
|
self.setColor(self.activeColor)
|
|
}
|
|
}
|
|
|
|
// 继续继承源子类,添加 message 并覆盖事件入口,保留 TextBlock 的原生能力。
|
|
class CustomStatus: StatusText {
|
|
message: String
|
|
|
|
// 显式调用基类 setText 以示范原生未绑定方法的接收者传递,再用继承方法改颜色。
|
|
function activate(self: CustomStatus, event: Event): Unit {
|
|
// 接收的是按钮转发的同一 Event;显式基类方法调用只传目标 self 和新文本,不把 Event 当作文本参数。
|
|
TextBlock.setText(self, self.message)
|
|
self.setColor(self.activeColor)
|
|
}
|
|
}
|
|
|
|
// 按钮保存目标 Block,事件到来时把同一 Event 转交给目标的动态 activate。
|
|
class ActionButton: Button {
|
|
target: Block
|
|
|
|
// 不复制目标或创建新展示对象,直接调用目标业务方法,选择实际类型的覆盖实现。
|
|
function activate(self: ActionButton, event: Event): Unit {
|
|
// button.onClick(button.activate) 派发进入这里,target 实际为 CustomStatus,动态查找其 activate 覆盖。
|
|
self.target.activate(event)
|
|
}
|
|
}
|
|
|
|
// Page 也可增加源字段;caption 用于构建页内标题,不替代原生窗口标题。
|
|
class CustomPage: Page {
|
|
caption: String
|
|
}
|
|
|
|
// 前面位置参数供原生 TextBlock 构造,后面的颜色/消息供继承字段;所有引用都保留托管类型。
|
|
label = CustomStatus("点击按钮更新同一个自定义文本对象", 2, "#242422", "#237A45", "源码子类与原生载荷共同工作")
|
|
button = ActionButton("调用源码子类方法", label)
|
|
button.onClick(button.activate)
|
|
// caption 是 CustomPage 的源构造字段;下面 render 只把它的当前字符串用于标题 TextBlock,没有注册 caption 绑定。
|
|
page = CustomPage("Endor 原生类型的源码继承")
|
|
// label 在根孩子中引用两次而不复制,setText/setColor 一次写入会刷新两处出现;末尾 textOf 单独展示 message 字段。
|
|
page.render(Column(24, 16, TextBlock(page.caption, 2), label, label, button, ui.textOf(label, "message")))
|