专业编程基础技术教程

网站首页 > 基础教程 正文

HarmonyOS Next 鸿蒙自学笔记系列三:ArkTS语法一

ccvgpt 2024-11-19 02:14:04 基础教程 7 ℃

十年老Android开发最近失业,在家自学HarmonyOS做笔记系列,计划两周学完,如有错误地方请大家指出,及时修改,一起学习。

ArkTS

ArkTS是在TS基础上扩展的,兼容TS和JS语法习惯,有JS基础的前端大佬会比较容易理解。

HarmonyOS Next 鸿蒙自学笔记系列三:ArkTS语法一

下面两个是ArkTS新增的约束,个人还是比较容易理解的。

1、强化静态类型检查,在编译阶段必须指定类型。

2、不允许在运行阶段更改对象布局。比如定义一个User类,里面有name和age,那么就只有这两个属性字段,不允许动态增加未定义的属性字段。


声明类型

1、变量 变量允许在运行阶段修改

let hello: string = "Hello, word!"
hello = "hahaha..."

2、常量

const hello: string = "Hello, word!"

运行阶段无法更改。这个java中的final以及kotlin中const val是比较类似的。


数据类型

1、基本类型:string number boolean

  • string 双引号单引号都可,另外支持模板字面量(Template Literals),使用反引号括起来即可(键盘ESC下面那个按键)。
let str: string= "字符串"
let str = '字符串'

let a = "001"
let str3 = `字符串 ${a}`
  • number
let num1:number = 1 //整数
let num2 = 0.1 //小数
let num3 = .1 //小数 可省略小数点前的0
let num4 = 0x01 //16进制
let num5 = 0b11 //二进制
  • boolean
let result: boolean = true  逻辑符 true & false

ArkTS支持自动推断类型,初始值类型即类型属性。


2、引用类型 Object Array 自定义对象等

  • Object 所有类型的基类
let obj: Object = 1
let obj_number: number = 1
  • Array

两种定义方法:

1、使用方括号

let array: string[] = ["", ""];
array[0] = "1"
array[1] = "2"
array.push("3") //新增加一个item

2、使用泛型数组类型

let array2: Array<string> = ["", ""];
array2[0] = "1"
array2[1] = "2"
array2.push("3") //新增加一个item

一些基础方法:

for循环

    //使用for循环
    for (let index = 0; index < array2.length; index++) {
      console.log(`item is ${array2[index]}`);
    }

    //使用for...of循环
    for (let item of array2) {
      console.log(`item is ${item}`);
    }

    //增强for循环
    array2.forEach(item => {
      console.log(`item is ${item}`);
    })


  • 自定义对象
export class TestAskTs {
  name: string = "";
  age: number = 1;

  constructor(name: string, age: number) {
    this.name = name
    this.age = age
  }

  print(): void {
    console.log(`print--> ${this.name}`);
  }
}

import { TestAskTs as Item, TestAskTs } from './TestAskTs'

let testItem = new TestAskTs("xiao", 2)


3、枚举 Enum

  • 不指定后缀值默认是number类型,赋值从0自增,参考Color :Color.red = 0 ,Color.white = 1 , Color.black = 2,
  • 显式指定,可自定义赋值,比如Color2。

一个枚举中的所有的item类型是相同的,比如第一个显式赋值为string,其他item不可以为其他类型。

enum Color {
  red,
  white,
  black
}

enum ColorStr {
  red = "red",
  white = "white",
  black = "black"
}

let c1: number = Color.red   // 输出值 0
let c2: string = ColorStr.red // 输出值 red


4、联合类型Union

Union:允许变量的类型为多个,比如同时定义为string number boolean等。

最终类型为最后一次赋值的类型,可通过typeof获取最终类型。

let un: string | number | boolean = "string"
un = false
un = 0.1

console.log(`Union test: ${un}`);
if (typeof un == "string") {
  console.log(`Union test string: ${un}`);
} else if (typeof un == "number") {
  console.log(`Union test number: ${un}`);
} else if (typeof un == "boolean") {
  console.log(`Union test boolean: ${un}`);
}

//打印结果
Union test: 0.1
Union test number: 0.1

ArkTS不允许值为空,想定义一个为null的对象时,可以使用Union完成,多增加一个null的类型即可。

let unNull: string | number | boolean | null = null


5、类型别名Aliases

给一组类型取一个别名,方便引用。

interface AliasesItem {
  name: string;
  age: number;
}

type AItem = AliasesItem
type AStr = string | number

本篇完事,各位再见。

#鸿蒙#

Tags:

最近发表
标签列表