HarmonyOS 应用开发之自定义组件成员属性访问限定符使用限制_harmonyos private,public

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数HarmonyOS鸿蒙开发工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年HarmonyOS鸿蒙开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img

img
img
htt

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上HarmonyOS鸿蒙开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新

如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注鸿蒙获取)
img

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

Property ‘builder_value’ is private and can not be initialized through the component constructor.
Property ‘regular_value’ is private and can not be initialized through the component constructor.


2.当成员变量被public访问限定符和@StorageLink/@StorageProp/@LocalStorageLink/@LocalStorageLink/@Consume装饰器同时修饰时,ArkTS会进行校验并产生告警日志。



@Entry
@Component
struct AccessRestrictions {
@Provide consume_value: string = “Hello”;
build() {
Column() {
ComponentChild()
}
.width(‘100%’)
}
}

@Component
struct ComponentChild {
@LocalStorageProp(“sessionLocalProp”) public local_prop_value: string = “Hello”;
@LocalStorageLink(“sessionLocalLink”) public local_link_value: string = “Hello”;
@StorageProp(“sessionProp”) public storage_prop_value: string = “Hello”;
@StorageLink(“sessionLink”) public storage_link_value: string = “Hello”;
@Consume public consume_value: string;
build() {
Column() {
Text(“Hello”)
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
}
}


编译告警日志如下:



Property ‘local_prop_value’ can not be decorated with both @LocalStorageProp and public.
Property ‘local_link_value’ can not be decorated with both @LocalStorageLink and public.
Property ‘storage_prop_value’ can not be decorated with both @StorageProp and public.
Property ‘storage_link_value’ can not be decorated with both @StorageLink and public.
Property ‘consume_value’ can not be decorated with both @Consume and public.


3.当成员变量被private访问限定符和@Link/@ObjectLink装饰器同时修饰时,ArkTS会进行校验并产生告警日志。



@Entry
@Component
struct AccessRestrictions {
@State link_value: string = “Hello”;
@State objectLink_value: ComponentObj = new ComponentObj();
build() {
Column() {
ComponentChild({link_value: this.link_value, objectLink_value: this.objectLink_value})
}
.width(‘100%’)
}
}

@Observed
class ComponentObj {
count: number = 0;
}
@Component
struct ComponentChild {
@Link private link_value: string;
@ObjectLink private objectLink_value: ComponentObj;
build() {
Column() {
Text(“Hello”)
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
}
}


编译告警日志如下:



Property ‘link_value’ can not be decorated with both @Link and private.
Property ‘objectLink_value’ can not be decorated with both @ObjectLink and private.


4.当成员变量被protected访问限定符修饰时,ArkTS会进行校验并产生告警日志。



@Entry
@Component
struct AccessRestrictions {
build() {
Column() {
ComponentChild({regular_value: “Hello”})
}
.width(‘100%’)
}
}

@Component
struct ComponentChild {
protected regular_value: string = “Hello”;
build() {
Column() {
Text(“Hello”)
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
}
}


编译告警日志如下:



The member attributes of a struct can not be protected.


5.当成员变量被private访问限定符、@Require和@State/@Prop/@Provide/@BuilderParam装饰器同时修饰时,ArkTS会进行校验并产生告警日志。



@Entry
@Component
struct AccessRestrictions {
build() {
Column() {
ComponentChild({prop_value: “Hello”})
}
.width(‘100%’)
}
}
@Component
struct ComponentChild {
@Require @Prop private prop_value: string = “Hello”;
build() {
Column() {
Text(“Hello”)
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
}
}


编译告警日志如下:



Property ‘prop_value’ can not be decorated with both @Require and private.
Property ‘prop_value’ is private and can not be initialized through the component constructor.


**为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:[`https://qr21.cn/FV7h05`]( )**


### 《鸿蒙开发学习手册》:


#### **如何快速入门:[`https://qr21.cn/FV7h05`]( )**


1. 基本概念
2. 构建第一个ArkTS应用
3. ……


![](https://img-blog.csdnimg.cn/img_convert/afd6b98a09014e557566dec0fd065c41.webp?x-oss-process=image/format,png)


#### **开发基础知识:[`https://qr21.cn/FV7h05`]( )**


1. 应用基础知识
2. 配置文件
3. 应用数据管理
4. 应用安全管理
5. 应用隐私保护
6. 三方应用调用管控机制
/img-blog.csdnimg.cn/img_convert/afd6b98a09014e557566dec0fd065c41.webp?x-oss-process=image/format,png)


#### **开发基础知识:[`https://qr21.cn/FV7h05`]( )**


1. 应用基础知识
2. 配置文件
3. 应用数据管理
4. 应用安全管理
5. 应用隐私保护
6. 三方应用调用管控机制
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值