当前位置: 首页 > news >正文

鸿蒙移动应用开发--ArkTS语法进阶实验

任务一使用“展开运算符 ...”对自行建立的两个数组实现合并。

1、定义两个数组:

  • 在代码中定义两个数组 array1 和 array2,分别包含一些元素。
  • 例如:array1 = [1, 2, 3] 和 array2 = [4, 5, 6]。

2、使用展开运算符合并数组:

  • 使用展开运算符 ... 将两个数组的元素展开并合并到一个新的数组 combinedArray 中。
  • 代码示例:let combinedArray = [...array1, ...array2];

3、输出合并后的数组:

  • 使用 console.log 输出合并后的数组,验证结果是否正确。
  • 预期输出:[1, 2, 3, 4, 5, 6]。

源代码

// 任务一:合并数组
let array1 = [1, 2, 3]
let array2 = [4, 5, 6]
console.log('原始数组1:',(array1).toString())
console.log('原始数组2:',(array2).toString())
let combinedArray = [...array1, ...array2] // 使用展开运算符合并数组
console.log('合并数组:',(combinedArray).toString())// 输出:[1, 2, 3, 4, 5, 6]
@Entry
@Component
struct Index {@State message: string = 'Hello World';build() {RelativeContainer() {Text(this.message).id('HelloWorld').fontSize($r('app.float.page_text_font_size')).fontWeight(FontWeight.Bold).alignRules({center: { anchor: '__container__', align: VerticalAlign.Center },middle: { anchor: '__container__', align: HorizontalAlign.Center }}).onClick(() => {this.message = 'Welcome';})}.height('100%').width('100%')}
}

运行截图:


任务二使用“剩余参数”实现多个(数量不限)整数相加求和。

1、定义求和函数:

  • 使用剩余参数 ...numbers 定义一个函数 sum,该函数接受任意数量的整数参数。
  • 例如:const sum = (...numbers: number[]): number => { ... };

2、实现求和逻辑:

  • 使用数组的 reduce 方法对传入的参数进行累加求和。
  • 代码示例:return numbers.reduce((acc, num) => acc + num, 0);

3、调用函数并输出结果:

  • 调用 sum 函数,传入多个整数参数,并使用 console.log 输出结果。
  • 例如:console.log(sum(1, 2, 3, 4, 5));

源代码:

 

// 任务二:使用剩余参数实现求和
const sum = (...numbers: number[]): number => {return numbers.reduce((acc, num) => acc + num, 0);
};
console.log('剩余参数实现求和:',(sum(1, 2, 3, 4, 5)).toString()); // 输出:15
console.log('剩余参数实现求和:',(sum(10, 20, 30)).toString()); // 输出:60
@Entry
@Component
struct Index {@State message: string = 'Hello World';build() {RelativeContainer() {Text(this.message).id('HelloWorld').fontSize($r('app.float.page_text_font_size')).fontWeight(FontWeight.Bold).alignRules({center: { anchor: '__container__', align: VerticalAlign.Center },middle: { anchor: '__container__', align: HorizontalAlign.Center }}).onClick(() => {this.message = 'Welcome';})}.height('100%').width('100%')}
}

运行截图:


任务三创建一个class 类,使用“extends”实现子类继承父类,使用“readonly”“private”“protected”“public”这四个修饰符,体会类的访问权限。

1、定义父类:

  • 创建一个父类 Parent,包含以下成员:
    1. public 成员变量 name:可以在类外部访问。
    2. protected 成员变量 age:只能在类内部和子类中访问。
    3. private 成员变量 gender:只能在类内部访问。
    4. readonly 成员变量 role:只读属性,初始化后不可修改。
  • 定义构造函数初始化这些成员变量。
  • 定义 public 方法 displayInfo,用于输出基本信息。
  • 定义 protected 方法 displayProtectedInfo,用于输出受保护的信息。
  • 定义 private 方法 displayPrivateInfo,用于输出私有信息。

2、定义子类:

  • 创建一个子类 Child,继承父类 Parent。
  • 在子类中调用父类的构造函数,并尝试访问父类的成员变量和方法。
  • 定义一个 public 方法 displayChildInfo,用于调用父类的 public 和 protected 方法。

3、测试类的继承和访问权限:

  • 创建父类和子类的实例。
  • 调用父类和子类的方法,验证访问权限是否符合预期。
  • 使用 console.log 输出结果。

源代码:

// 父类
class Parent {public name: string;protected age: number;private gender: string;readonly role: string;constructor(name: string, age: number, gender: string, role: string) {this.name = name;this.age = age;this.gender = gender;this.role = role;}public displayInfo() {console.log(`Name: ${this.name}, Age: ${this.age}, Role: ${this.role}`);}protected displayProtectedInfo() {console.log(`Protected Info: ${this.age}`);}private displayPrivateInfo() {console.log(`Private Info: ${this.gender}`);}
}
// 子类
class Child extends Parent {constructor(name: string, age: number, gender: string, role: string) {super(name, age, gender, role);}public displayChildInfo() {this.displayInfo(); // 可以访问父类的 public 方法this.displayProtectedInfo(); // 可以访问父类的 protected 方法// this.displayPrivateInfo(); // 无法访问父类的 private 方法}
}
// 任务三:测试类的继承和访问权限
let parent = new Parent('John', 30, 'Male', 'Developer');
let child = new Child('Alice', 25, 'Female', 'Designer');
parent.displayInfo(); // 输出:Name: John, Age: 30, Role: Developer
child.displayChildInfo(); // 输出:Name: Alice, Age: 25, Role: Designer
// child.displayProtectedInfo(); // 错误:无法直接访问父类的 protected 方法
@Entry
@Component
struct Index {@State message: string = 'Hello World';build() {RelativeContainer() {Text(this.message).id('HelloWorld').fontSize($r('app.float.page_text_font_size')).fontWeight(FontWeight.Bold).alignRules({center: { anchor: '__container__', align: VerticalAlign.Center },middle: { anchor: '__container__', align: HorizontalAlign.Center }}).onClick(() => {this.message = 'Welcome';})}.height('100%').width('100%')}
}

 运行截图:

相关文章:

  • BOTA新六维力传感器PixONE:用12维度力矩与运动感测,驱动人形机器人力控未来
  • 电子病历高质量语料库构建方法与架构项目(智能数据目录篇)
  • 数据隐私在Web3环境下的重要性及实现方法
  • 蓝牙语音遥控国产适用芯片HS6621
  • 适配 AGP8.5,maven 私服发布报错(七)
  • 鸿蒙文件上传-从前端到后端详解,对比jq请求和鸿蒙arkts请求区别,对比new FormData()和鸿蒙arktsrequest.uploadFile
  • 从高端制造到民生场景:天元轻量化软件的“破局”之路
  • Access开发:轻松一键将 Access 全库表格导出为 Excel
  • 【C++QT】Combo Box 组合框控件详解
  • 关于vue+iview中tabs嵌套及实际应用
  • 分布式数字身份:迈向Web3.0世界的通行证 | 北京行活动预告
  • 全球合规风暴升级:韩国原产地稽查揭示跨境电商生死线
  • electron+vite+vue3 快速入门教程
  • 基于DeepSeek与HTML的可视化图表创新研究
  • 蛋白质数据库InterPro介绍
  • 《跨端开发变革者:解码阿里Ant Container Engine的底层逻辑》
  • ESP32开发-作为TCP客户端发送数据到网络调试助手
  • Stack--Queue 栈和队列
  • 抢先体验 | Qwen3 模型发布:基于 ZStack AIOS 平台极速体验
  • AI数字人:人类身份与意识的终极思考(10/10)
  • 今年五一假期出游人群规模预计比去年提升8%,哪里最热门?
  • 两部门调度部署“五一”假期安全防范工作,要求抓好旅游安全
  • 十四届全国人大常委会举行第四十四次委员长会议
  • 国家卫健委有关负责人就白皮书发布答记者问
  • 是否进行了及时有效处置?伤者情况如何?辽阳市相关负责人就饭店火灾事故答问
  • 孕妇乘坐高铁突发临产,广西铁路部门协助送医平安产子