在这里插入图片描述


参考文档 : <HarmonyOS第一课>ArkTS开发语言介绍





一、TypeScript 类




1、创建类语法


TypeScript 语言 支持 面向对象 编程 , 下面介绍如何定义 TypeScript 类 ;

使用 class 类名 , 声明一个类 ;

class Student {}

在类中 , 定义 成员属性 不需要使用 let 或 var 关键字 , 直接声明即可 , 可 在 成员属性前面 使用 private / public / protected 访问限定符 ;

同时 , 定义类的成员属性时 , 必须指定 该成员的类型 , 并进行初始化 ;

    // 定义类的 成员属性  
    public name: string = "";  
    private age: number = 0;  

TypeScript 类的 构造函数 , 使用 constructor 关键字定义 , 在 参数列表 中需要 指定形参 和 形参类型 ;

    // 构造函数  
    constructor(name: string, age: number) {  
        this.name = name;  
        this.age = age;  
    }

定义 TypeScript 类的 成员方法 时 , 不需要使用 function 关键字 , 直接使用 方法名(){} 进行定义 , 如果方法有参数和返回值 , 需注明类型 ;

    // 定义类的 成员方法
    hello() {
        console.log(this.name + " is " + this.age + " years old");
    }

创建 TypeScript 类对象时 , 使用 new 关键字创建 类对象 ;

// 创建 Student 类对象
let student: Student = new Student("Jerry", 12);

创建对象后 , 使用 . 操作符 , 调用对象的成员 ;

// 调用 Student 对象的成员方法
student.hello();

2、代码示例 - 类的创建和使用


代码示例 :

class Student {  
    // 定义类的 成员属性  
    public name: string = "";  
    private age: number = 0;  

    // 构造函数  
    constructor(name: string, age: number) {  
        this.name = name;  
        this.age = age;  
    }

    // 定义类的 成员方法
    hello() {
        console.log(this.name + " is " + this.age + " years old");
    }
}

// 创建 Student 类对象
let student: Student = new Student("Jerry", 12);

// 调用 Student 对象的成员方法
student.hello();

https://ts.nodejs.cn/play 中运行 TypeScript 代码 :

[LOG]: "Jerry is 12 years old" 

在这里插入图片描述





二、TypeScript 子类使用 extends 继承父类



TypeScript 类 可以通过使用 extends 关键字 , 继承 父类的 成员属性 和 成员方法 , 使得子类具有父类 的特征 ;


继承代码示例 :

class Student {  
    // 定义类的 成员属性  
    public name: string = "";  
    public age: number = 0;  

    // 构造函数  
    constructor(name: string, age: number) {  
        this.name = name;  
        this.age = age;  
    }

    // 定义类的 成员方法
    hello() {
        console.log(this.name + " is " + this.age + " years old");
    }
}

// 定义子类 继承 父类
class Employee extends Student {
    // 子类自己的方法
    private skill: String = "";

    // 子类构造函数
    constructor(name: string, age: number, skill: String) {
        super(name, age);
        this.skill = skill;
    }

    // 子类成员方法 
    hello() {
        console.log(this.name + " is " + this.age + " years old , skill is " + this.skill);
    }
}

// 创建 Student 类对象
let student: Student = new Student("Jerry", 12);

// 调用 Student 对象的成员方法
student.hello();


// 创建 Employee 类对象
let employee: Employee = new Employee("Tom", 18, "Speak English");

// 调用 Employee 对象的成员方法
employee.hello();

https://ts.nodejs.cn/play 中运行 TypeScript 代码 :

[LOG]: "Jerry is 12 years old" 
[LOG]: "Tom is 18 years old , skill is Speak English" 

在这里插入图片描述





三、迭代器遍历




1、可迭代类型说明


在 TypeScript 中如果一个对象 实现了 Symbol.iterator 属性后 , 就可以使用 for 循环 进行迭代 , TypeScript 语言内置的可迭代类型有 :

  • Array 数组
  • Map 映射
  • Set 集合
  • String 字符串
  • Int32Array 4 字节整型数组
  • Unit32Array

for 循环遍历有 2 种方式 :

  • for of 语句遍历的是 元素 ;
  • for in 语句遍历的事 下标 ;

2、for of 语句遍历数组元素


使用 for of 循环语句 , 可以对数组元素进行遍历 ;

代码示例 :

let colors: String[] = ["Blue", "Red", "Green"];

// 使用 for of 遍历数组
for (let color of colors) {
    console.log(color);
}

https://ts.nodejs.cn/play 中运行 TypeScript 代码 :

[LOG]: "Blue" 
[LOG]: "Red" 
[LOG]: "Green" 

在这里插入图片描述


3、for in 语句遍历数组下标


使用 for in 循环语句 , 可以对数组 下标 进行遍历 ;

代码示例 :

let colors: String[] = ["Blue", "Red", "Green"];

// 使用 for in 遍历数组下标
for (let index in colors) {
    console.log(index + " . " + colors[index]);
}

https://ts.nodejs.cn/play 中运行 TypeScript 代码 :

[LOG]: "0 . Blue" 
[LOG]: "1 . Red" 
[LOG]: "2 . Green" 

在这里插入图片描述

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐