认识 ArkTS 语法

在这里插入图片描述

ArkTS语法就是ts、js语法的超集,引入ArkUI框架(声明式语句),

**变量常量的声明和使用


//变量的声明
let hi: string = 'hello';
hi = 'hello, world';

//常量的声明,只能被复制一次
const hello: string = 'hello';

//自动类型推断,由于ArkTS是一种静态类型语言,所有数据的类型都必须在编译时确定
let hi1: string = 'hello';
let hi2 = 'hello, world';

**函数的声明和使用


if (condition1) {
  // 语句1
} else if (condition2) {
  // 语句2
} else {
  // else语句
}

let s1 = 'Hello';
if (s1) {
  console.log(s1); // 打印“Hello”
}

let s2 = 'World';
if (s2.length != 0) {
  console.log(s2); // 打印“World”
}

switch (expression) {
  case label1: // 如果label1匹配,则执行
    // ...
    // 语句1
    // ...
    break; // 可省略
  case label2:
  case label3: // 如果label2或label3匹配,则执行
    // ...
    // 语句23
    // ...
    break; // 可省略
  default:
    // 默认语句
}

condition ? expression1 : expression2

for ([init]; [condition]; [update]) {
  statements
}

for (forVar of expression) {
  statements
}

let n = 0;
let x = 0;
while (n < 3) {
  n++;
  x += n;
}

let i = 0;
do {
  i += 1;
} while (i < 10)

let x = 0;
while (true) {
  x++;
  if (x > 5) {
    break;
  }
}

let sum = 0;
for (let x = 0; x < 100; x++) {
  if (x % 2 == 0) {
    continue
  }
  sum += x;
}

try {
  // 可能发生异常的语句块
} catch (e) {
  // 异常处理
}

function processData(s: string) {
  let error: Error | null = null;

  try {
    console.log('Data processed: ' + s);
    // ...
    // 可能发生异常的语句
    // ...
  } catch (e) {
    error = e as Error;
    // ...
    // 异常处理
    // ...
  } finally {
    if (error != null) {
      console.log(`Error caught: input='${s}', message='${error.message}'`);
    }
  }
}


function add(x: string, y: string): string {
  let z: string = `${x} ${y}`;
  return z;
}

在函数声明中,必须为每个参数标记类型。如果参数为可选参数,那么允许在调用函数时省略该参数。函数的最后一个参数可以是rest参数

箭头函数
let sum = (x: number, y: number): number => {
  return x + y;
}

**类的声明和使用(体现面向对象特性 封装 继承 多态)


//number和Number类型
let n1 = 3.14;
let n2 = 3.141592;
let n3 = .5;
let n4 = 1e10;

function factorial(n: number): number {
  if (n <= 1) {
    return 1;
  }
  return n * factorial(n - 1);
}

//布尔类型
let isDone: boolean = false;

// ...

if (isDone) {
  console.log ('Done!');
}

//String类型
let s1 = 'Hello, world!\n';
let s2 = 'this is a string';
let a = 'Success';
let s3 = `The result is ${a}`;

//Void类型,void类型用于指定函数没有返回值,此类型只有一个值,同样是void。由于void是引用类型,因此它可以用于泛型类型参数
class Class<T> {
  //...
}
let instance: Class <void>


//**Object类型**
Object类型是所有引用类型的基类型。任何值,包括基本类型的值(它们会被自动装箱),都可以直接被赋给Object类型的变量。

//**Array类型**
let names: string[] = ['Alice', 'Bob', 'Carol'];

//**Enum类型**
enum ColorSet { Red, Green, Blue }
let c: ColorSet = ColorSet.Red;
//常量表达式可以用于显式设置枚举常量的值。
enum ColorSet { White = 0xFF, Grey = 0x7F, Black = 0x00 }
let c: ColorSet = ColorSet.Black;


//**Union类型**
class Cat {
  // ...
}
class Dog {
  // ...
}
class Frog {
  // ...
}
type Animal = Cat | Dog | Frog | number
// Cat、Dog、Frog是一些类型(类或接口)

let animal: Animal = new Cat();
animal = new Frog();
animal = 42;
// 可以将类型为联合类型的变量赋值为任何组成类型的有效值

//**Aliases类型**

Aliases类型为匿名类型(数组、函数、对象字面量或联合类型)提供名称,或为已有类型提供替代名称。
type Matrix = number[][];
type Handler = (s: string, no: number) => string;
type Predicate <T> = (x: T) => Boolean;
type NullableObject = Object | null;

可见性修饰符:private public protect



**运算符

复合赋值运算符将赋值与运算符组合在一起,其中x op = y等于x = x op y
一元运算符为-、+、–、++
二元元算符:±*/
逻辑运算符:与或非
位运算符:与或非 疑惑 左右移

**对象字面量

class C {
  n: number = 0
  s: string = ''
}

let c: C = {n: 42, s: 'foo'};

**接口(非java中的标准接口,该接口可以有属性)

interface Style {
  color: string // 属性
}
interface AreaSize {
  calculateAreaSize(): number // 方法的声明
  someMethod(): void;     // 方法的声明
}

**模块

有导出导入的概念,导入分为静态导入和动态导入
程序可划分为多组编译单元或模块。

每个模块都有其自己的作用域,即,在模块中创建的任何声明(变量、函数、类等)在该模块之外都不可见,除非它们被显式导出。

与此相对,从另一个模块导出的变量、函数、类、接口等必须首先导入到模块中。

**关键字

关键字this只能在类的实例方法中使用

class A {
  count: string = 'a'
  m(i: string): void {
    this.count = i;
  }
}

**声明式UI语法

自定义组件的概念

元素属性

容器:横向布局,纵向布局,row() colum()

ForEach语法渲染列表所有数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值