关于ArkTS的数据类型 原创

安苒anran0
发布于 2023-6-13 21:03
浏览
3收藏

关于ArkTS的数据类型

ArkTS是一门强类型语言,它与Python的强类型不同。它的数据类型是必须经过声明的。所以学习ArkTS非常重要的一步是知道ArkTS有哪些常用的数据类型。
源代码在附件处哦

布尔值(boolean)

boolean是最基本的数据类型,是逻辑判断的基础。
boolean 通常有两种状态 true、false
true 代表为真值
false代表为假值

  // 布尔值
  @State flag: boolean = true;
  build() {
    Row() {
      Column() {
        if (this.flag)
          Text("True")
        else
          Text("False")
      }.width('100%')
    }.height('100%')
  }

运行效果图
 关于ArkTS的数据类型-鸿蒙开发者社区

数字(number)

ArkTS内所有的数据类型都是浮点型,使用number声明,支持十进制,八进制,十六进制和二进制表示方式

  // 数字
  @State n1: number = 0; //十进制
  @State n2: number = 0.1; //十进制
  @State n3: number = 0x6060; //十六进制
  @State n4: number = 0b1010; //二进制
  @State n5: number = 0o114514; //八进制
  build() {
    Row() {
      Column() {
        Text(`${this.n1}`)
        Text(`${this.n2}`)
        Text(`${this.n3}`)
        Text(`${this.n4}`)
        Text(`${this.n5}`)
      }.width('100%')
    }.height('100%')
  }

 关于ArkTS的数据类型-鸿蒙开发者社区 关于ArkTS的数据类型-鸿蒙开发者社区

字符串(string)

与其他语言一样,使用string表示字符串。在为Text等处为字符串设置文本的时候会有类型检查,必须使用string才可以通过语法检查。

  // 字符串
  @State message: string = 'Hello'
  @State n: number = 10;
  @State message2: string = `${this.message} ,这也是字符串模版。${this.n}`

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Text(`你好呀,这个一个字符串模板:${this.n}。`);
        Text(this.message2)
      }.width('100%')
    }.height('100%')
  }

 关于ArkTS的数据类型-鸿蒙开发者社区

注意这里用到了一个有趣的语法,在上面被反引号(`)包围的字符串是ArkTS的模版字符串。它可以在多行文本内以${ expr }的格式内嵌字符串。

另外,被单引号( ’ )包裹起来的文字与和双引号( " )包裹起来的字符串一样是代表字符串

数组

数组是一系列数据的集合,在绝大部分编程语言中都存在数组的概念。

  // 数组
  @State ns1: number[] = [1, 2, 3];
  @State ns2: Array<number> = [5, 6, 7];

  build() {
    Row() {
      Column() {
        ForEach(this.ns1, (item) => {
          Text(`${item}`)
        })
        ForEach(this.ns2, (item) => {
          Text(`${item}`)
        })
      }.width('100%')
    }.height('100%')
  }

 关于ArkTS的数据类型-鸿蒙开发者社区

元组

元组在ArkTS中表现为一个已知数量和类型的数组,各元素之间的类型不必相同。

  // 元组
  @State x: [string, number] = ["hello", 123];
  build() {
    Row() {
      Column() {
        Text(`${typeof this.x[0]}  -  ${this.x[0]}`)
        Text(`${typeof this.x[1]}  -  ${this.x[1]}`)
      }.width('100%')
    }.height('100%')
  }

 关于ArkTS的数据类型-鸿蒙开发者社区

这里用到了"typeof"语法,可以将数据的类型获取出来。获取出来的数据为一个string类型的字符串。

动态类型(any)

存在一些情况,在编程阶段我们并不能给编译器一个明确的数据类型。那么我们可以使用any来表示数据类型。就比如any在一个杂糅着多种数据类型的数组里就十分好用。

  // 动态类型,ans
  @State ns1: any[] = [false,12, "你好", [123,2,3] ];

  build() {
    Row() {
      Column() {
        ForEach(this.ns1, (item, i) => {
          Text(`${i} - ${typeof item}  -  ${item}`)
        });
      }.width('100%')
    }.height('100%')
  }

 关于ArkTS的数据类型-鸿蒙开发者社区

void 类型

void类型的数据只允许被赋值null与undefined

  @State v1: void = null;
  @State v2: void = undefined;

  build() {
    Row() {
      Column() {
        Text(`${typeof this.v1}  -  ${this.v1}`)
        Text(`${typeof this.v2}  -  ${this.v2}`)
      }.width('100%')
    }.height('100%')
  }

 关于ArkTS的数据类型-鸿蒙开发者社区

Null和Undefined

null类型与undefined类型有许多相似之处

  • null类型只能被赋值null
  • undefined类型只能被赋值undefined
  // Null 和 Undefined
  @State n: null = null;
  @State u: undefined = undefined;
  build() {
    Row() {
      Column() {
        Text(`${typeof this.n}  -  ${this.n}`)
        Text(`${typeof this.u}  -  ${this.u}`)
      }.width('100%')
    }.height('100%')
  }

 关于ArkTS的数据类型-鸿蒙开发者社区

Nerver

never类型表示的是那些永不存在的值的类型。

  • never 类型除了被never类型赋值,无法被任何值赋值(包括null与undefined)
  • 任何值都可以被never类型赋值
  // Never不存在类型
  n: never ;
  build() {
    Row() {
      Column() {
        Text(`${typeof this.n}  -  ${this.n}`)
        // Text(`${typeof this.u}  -  ${this.u}`)
      }.width('100%')
    }.height('100%')
  }

 关于ArkTS的数据类型-鸿蒙开发者社区

对象型object

object 代表非原始类型数据。

  // 对象型Object
  @State v: object = { "hello": 123 }

  build() {
    Row() {
      Column() {

        Text(`${typeof this.v}  -  ${(this.v)}`)
        Text(`${typeof this.v["hello"]}  -  ${(this.v["hello"])}`)

      }.width('100%')
    }.height('100%')
  }

 关于ArkTS的数据类型-鸿蒙开发者社区

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
ArkTSTest.zip 292.94K 8次下载
已于2023-6-13 21:04:37修改
4
收藏 3
回复
举报
1条回复
按时间正序
/
按时间倒序
红叶亦知秋
红叶亦知秋

很实用的整理

1
回复
2023-6-14 10:37:23
回复
    相关推荐