ArkTS 是 HarmonyOS NEXT 的开发语言,它基于 TypeScript 并进行了扩展和优化。以下是一些基础语法知识点、示例用法及注意事项。
一、ArkTS 简介
ArkTS 是一种基于 TypeScript 的编程语言,主要用于 HarmonyOS 应用的 UI 界面和业务逻辑开发。它在 TypeScript 的基础上,进行了一些针对 HarmonyOS 系统的优化和定制。
二、创建 ArkTS 项目
打开 DevEco Studio:点击 File -> New -> Project,选择适合的模板作为项目类型。
输入项目名称和包名:然后点击 Next。
完成项目创建:点击 Finish,DevEco Studio 将自动创建一个 ArkTS 项目。
三、编写 ArkTS 代码
编写 UI 界面:ArkTS 项目的 UI 界面使用 XML 文件定义,可以在 resources/base/layout 目录下找到应用的布局文件。
编写业务逻辑:ArkTS 项目的业务逻辑代码使用 TypeScript 编写,可以在 src/main/js/default 目录下找到应用的 TypeScript 代码。
添加资源文件:ArkTS 项目的图片、字符串等资源文件存放在 resources/base 目录下。
配置文件:ArkTS 项目的配置信息存放在 config.json 文件中。
四、ArkTS 关键语法和使用示例
1. 类型注解
类型注解是 TypeScript 的核心特性之一,它允许在变量、函数参数和函数返回值上添加类型信息。
 let message: string = "Hello, HarmonyOS";let count: number = 10;function greet(name: string): string {    return `Hello, ${name}`;}let greeting: string = greet("HarmonyOS");
       复制代码
 2. 接口
接口是 TypeScript 中定义复杂类型的一种方式,它可以描述一个对象的结构。
 interface Person {    name: string;    age: number;}function showPersonInfo(person: Person) {    console.log(`Name: ${person.name}, Age: ${person.age}`);}let person: Person = { name: "John", age: 30 };showPersonInfo(person);
       复制代码
 3. 类
类是 TypeScript 中定义对象类型的一种方式,支持继承和多态。
 class Animal {    constructor(public name: string) {}    speak() {        console.log(`${this.name} makes a noise.`);    }}
class Dog extends Animal {    speak() {        console.log(`${this.name} barks.`);    }}
let dog = new Dog("Bingo");dog.speak(); // Bingo barks.
       复制代码
 4. 继承
继承允许一个类(子类)继承另一个类(父类)的属性和方法。
 class Base {    commonMethod() {        console.log("Common Method");    }}
class Derived extends Base {    derivedMethod() {        console.log("Derived Method");    }}
let derived = new Derived();derived.commonMethod(); // Common Methodderived.derivedMethod(); // Derived Method
       复制代码
 5. 泛型
泛型允许在定义函数、接口或类时使用类型参数。
 function identity<T>(arg: T): T {    return arg;}
let output = identity<string>("Hello, world!");console.log(output); // "Hello, world!"
       复制代码
 6. 模块
模块是 TypeScript 中组织代码的一种方式,支持导入和导出。
 // file1.tsexport function sayHello(name: string) {    console.log(`Hello, ${name}`);}
// file2.tsimport { sayHello } from "./file1";sayHello("TypeScript");
       复制代码
 7. 装饰器
装饰器是一种特殊类型的声明,它可以被附加到类、方法、属性或参数上。
 function log(target: any, propertyKey: string, descriptor: PropertyDescriptor) {    console.log(`Property ${propertyKey} is being called`);}
class Person {    @log    name() {        return "John";    }}
let person = new Person();person.name(); // Property name is being called
       复制代码
 8. 异步编程
异步编程允许你编写非阻塞的代码。
 async function fetchData() {    return await fetch("https://api.example.com/data");}
fetchData().then(data => console.log(data));
       复制代码
 9. 类型别名
类型别名允许你为类型定义一个新名称。
 type Name = string;type Coordinates = { x: number; y: number };
let name: Name = "Alice";let coordinates: Coordinates = { x: 10, y: 20 };
       复制代码
 10. 类型保护
类型保护是一种检查变量类型的方法,可以在编译时确保变量具有正确的类型。
 type Shape = Circle | Square;function getArea(shape: Shape): number {    if (shape instanceof Circle) {        return Math.PI * shape.radius ** 2;    } else {        return shape.width * shape.height;    }}
       复制代码
 11. 枚举
枚举是一种特殊的类型,它允许你为一组有限的值定义友好的名字。
 enum Direction {    Up,    Down,    Left,    Right,}
function move(direction: Direction): void {    console.log(`Moving in direction: ${Direction[direction]}`);}move(Direction.Up);
       复制代码
 12. 映射类型
映射类型允许你根据现有类型创建新的类型。
 type ReadonlyPoint = Readonly<Point>;let readonlyPoint: ReadonlyPoint = { x: 10, y: 20 };readonlyPoint.x = 30; // Error: Cannot assign to 'x' because it is a read-only property
       复制代码
 五、注意事项
类型检查:ArkTS 在编译时进行类型检查,可以在代码运行前发现和修复错误。
IDE 支持:由于有了类型信息,IDE 可以提供更好的自动完成、导航和重构功能。
模块化编程:ArkTS 支持模块化编程,可以将代码组织成模块,以便于管理和维护。
通过以上示例和注意事项,你可以更好地理解和掌握 ArkTS 的基础语法和使用方式。
评论