背景
加深對(duì)@Observed@ObjectLink 裝飾器使用的理解,以小故事做注釋
效果
代碼片段
這是一個(gè)故事
Boss為了交付兩個(gè)完全一樣的客戶項(xiàng)目【只要寫1萬行代碼】,在公司內(nèi)部,把一個(gè)程序員配給了項(xiàng)目經(jīng)理,讓其監(jiān)督寫代碼
項(xiàng)目經(jīng)理領(lǐng)到指示后, 先告知客戶一,公司為其配了專職程序員,由其隨時(shí)發(fā)號(hào)施令讓其敲代碼,然后又以同樣的話術(shù)告知客戶二
接下來的事情,客戶一和客戶二,他們都非常滿意,因?yàn)槊恳惶?,雖然他們各自只督促程序員寫了一行代碼,但每天下班時(shí),
客戶們都能看到兩行代碼,呵呵。Boss聽聞此事,甚是開心,遂告知項(xiàng)目經(jīng)理,也要在每天中督促一次程序員,果不其然,程序員竟然每天可以寫三行代碼了,
對(duì)此老板深思了一會(huì)......, 做了一個(gè)決定,將項(xiàng)目中的程序直接換掉, 僅留當(dāng)前的項(xiàng)目經(jīng)理.
later,客戶發(fā)現(xiàn)之前寫的代碼沒了,哭天罵娘神明英武的Boss一氣之下,為客戶們重新配了新的項(xiàng)目經(jīng)理和新的程序員
@Entry
@Component
struct Boss{
@State projectManager: ProjectManager = new ProjectManager(new Programmer(0, '小哥1'), '項(xiàng)管1');
build(){
Column({space: 50}){
Customer({ label: '客戶一', programmer: this.projectManager.programmer })
Customer({ label: '客戶二', programmer: this.projectManager.programmer })
Button(`this.projectManager.programmer.codelines+= 1`)
.onClick(() = > {
//Boss告知項(xiàng)目經(jīng)理督促程序員寫代碼
this.projectManager.programmer.codelines += 1;
})
Button(`this.projectManager.programmer = new Programmer(0)`)
.onClick(() = > {
//換程序員
this.projectManager.programmer = new Programmer(0, '小哥2');
})
Button(`this.projectManager = new ProjectManager(Programmer(0))`)
.onClick(() = > {
//換項(xiàng)目經(jīng)理,換程序員
this.projectManager = new ProjectManager(new Programmer(0, '小哥3'), '項(xiàng)管2');
})
}
.width('100%')
.height('100%')
.padding({top: px2vp(111)})
.alignItems(HorizontalAlign.Center)
.justifyContent(FlexAlign.Center)
.backgroundColor(Color.Pink)
}
}
@Observed
class Programmer {
public name: string
public codelines: number = 1;
constructor(c: number, d: string) {
this.codelines = c;
this.name = d;
}
}
class ProjectManager {
public name: string
public programmer: Programmer;
constructor(a: Programmer, b: string) {
this.programmer = a;
this.name = b;
}
}
@Component
struct Customer {
label: string = 'customer';
@ObjectLink programmer: Programmer;
build() {
Row() {
Button(`[${this.label}] 讓 ${this.programmer.name} 寫代碼 = ${this.programmer.codelines}`)
.onClick(() = > {
this.programmer.codelines += 1;
})
}.backgroundColor(Color.Green).padding('16vp')
}
}
審核編輯 黃宇
-
代碼
+關(guān)注
關(guān)注
30文章
4722瀏覽量
68236 -
鴻蒙OS
+關(guān)注
關(guān)注
0文章
188瀏覽量
4359
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論