鸿蒙 Harmony ArkTs开发教程三 流程控制

复制代码
关键字:
 if
else if
else
== 比较内容是否相等 / 较数据类型是否一致 不一致就会强制类型转换
=== 比较内容是否相等
& 前面为false 后面也会执行
&& 并且前面为true 后面也得为true
| 或 无论前面是否为true 都执行后面的判断
|| 或者 前面为true 后面则不执行
% 取模
// == 比较数据类型是否一致 不一致就会强制类型转换
// typeScript中 '' 空字符串 , 0 数字0 ,'null' null,undefined 都会被认定为false ,其他为true

switch(key) case value : break 基本等同于Java / JavaScript

复制代码
// @ts-nocheck
// @ts-nocheck
import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';

export default class EntryAbility extends UIAbility {
  onCreate(want, launchParam) {
    let age = 18 //隐式声明一个number类型
    let state = true //隐式声明一个boolean类型
    if (age % 2 === 0) {
      // == 比较数据类型是否一致 不一致就会强制类型转换
      //===
      // typeScript中 '' 空字符串 , 0 数字0 ,'null' null,undefined 都会被认定为false ,其他为true

      console.log("偶数");
    } else {
      console.log("奇数");
    }

    //if 如果...条件成立 &&并且. 后面为true 则执行
    if (age > 18 && age < 66 && state) {
      console.log("符合条件满18 并且小于66 并且 state 为true");
    }

    if (age > 0) {
      console.log("age > 0");
    }
    else if (age < 0) {
      console.log("age < 0");
    } else {
      console.log("age = 0");
    }

    let name = undefined
    if (name) {
      //如果不是 undefined  , null , 'null' , '', 0 则会执行
    }

    let value = 'A'
    switch (value) {
      case 'A':
        console.log('isA')
        break
      case 'B':
        console.log('isB')
        break
      default:
        console.log('isOther')
        break;
    }

    let index = 0
    while (index < 10) {
      index++
      console.log(index.toString());
    }

    let names = ['A', 'B']

    //普通循环
    for (let i = 0; i < names.length; i++) {
      console.log(names[i]);
    }

    //forEach 根据索引
    for (const i in names) {
      console.log(names[i]);
    }

    //forEach 根据值
    for (const name of names) {
      console.log(name)
    }

    //names.forEach()

    //function 定义方法
    function eat(name: string): void {
      console.log("eat" + name)
    }

    function add(a: number, b: number): number {
      return a + b
    }

    //等同于 add
    function add1(a: number, b: number) {
      return a + b
    }

    eat('apple')
    console.log(add1(1, 2).toString());

    //简写版function
    let sayHi = (name: string) => {
      console.log('Hi' + name);
    }

    let getValue = (value: number) => {
      return value++
    }

    sayHi('summer')
    console.log(getValue(1).toString())

    //传参 可选参数
    let sayHi2 = (name?: string) => {
      name = name ? name : 'test'
      console.log(name);
    }

    //不传参 使用默认参数
    let sayHi3 = (name: string = 'World') => {
      name = name ? name : 'test'
      console.log(name);
    }


    //枚举
    enum T {
      T1 = "T1",
      T2 = "T2",
      T3 = "T3"
    }

    //枚举 不写值 默认是 1-3
    enum T1 {
      T1,
      T2,
      T3
    }

    //接口
    interface IA {
      onClickListener(view: number): void
    }

    //定义类 实现接口
    class Iimp implements IA{
      constructor() {
        //构造方法 等同于Kotlin
      }
      onClickListener(view : number) : void{
        console.log("implements method");
      }
    }

    let typeA : Iimp = new IA()
    typeA.onClickListener(1)


    class A1 {
      private valueA1 : number
      private valueA2 : number
      constructor(index : number,index1 : number) {
        this.valueA1 = index;
        this.valueA2 = index1;
      }

      public area() : number{
        return this.valueA1;
      }
    }

    //extends 继承
    class A2 extends A1{
      constructor(index: number) {
        super(index,index)
      }

    }

    let a2 : A2 = new A2(1)
    a2.area()

    //模块开发 功能抽取 .ts文件  每个文件都是一个模块
    //export 表示可以导出
    export class BaseUtils{

    }
    //导出方法
    export function addNumber():void{

    }

    import {BaseUtils,addNumber} form '../Test2' //引用类 或者方法
  





  }
}
相关推荐
donk131415 分钟前
React Native for OpenHarmony 实战:三方库 react-native-segmented-control-tab 的鸿蒙化适配指南
javascript·react native·react.js·华为·harmonyos
360智汇云1 小时前
鸿蒙开发中如何实现画中画
华为·harmonyos
donk13143 小时前
React Native for OpenHarmony 实战:三方库 react-native-toast-message 的鸿蒙化适配指南
react native·react.js·音视频·harmonyos
Flutter_OH3 小时前
Flutter OHOS Dart 堆栈符号化
harmonyos
Flutter_OH3 小时前
Flutter OHOS Flutter 多设备自适应 DPI 指导文档
harmonyos
SuperHeroWu74 小时前
【HarmonyOS 7】 应用 Agent 接入云 A2A 与端 A2A 的实践详解
ai·harmonyos·小艺
用户29469405448164 小时前
个人开发者上架鸿蒙 AI 应用,最后倒在安全评估实名制这一格
harmonyos·deepseek
Keya5 小时前
HarmonyOS 开发实战:用 util.Aspect 玩转 AOP 切面编程
harmonyos
威哥爱编程6 小时前
HarmonyOS 7 Agent 开发实战 03:Tool Calling 让 AI 查实时订单与库存
agent·harmonyos
威哥爱编程6 小时前
HarmonyOS 7 Agent 开发实战 02:Agent 工程脚手架与对话 UI 骨架
agent·harmonyos