ArkTS层实例化与单例介绍

APP里有不同的容器, 不同的容器都需要引入MSI,那为了不让不同容器内相关的功能互相串,就需要确保我们功能的载体class不能是相同的单实例。那现在的问题是,如果在写class的位置导出实例,那就是单实例;如果想在注册APIClass的时候实例化,现在应该如何处理呢。

HarmonyOS
2024-05-26 17:59:06
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
橘猫bbt7

使用的核心API

class单例、实例化应用,及变量的map、push。

核心代码解释

类的单例定义:

export class ClassA { 
  testvalue: number = 1; 
  constructor() { 
  } 
 
  onRegister(){ 
    this.testvalue = 2; 
  } 
 
  setValue(value: number){ 
    this.testvalue = value; 
  } 
 
  getValue():number{ 
    return this.testvalue; 
  } 
} 
export default new ClassA();
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

实例化类定义:

export class ClassD { 
  testvalue: number = 1; 
 
  constructor() { 
  } 
 
  onRegister() { 
    this.testvalue = 2; 
  } 
 
  setValue(value: number) { 
    this.testvalue = value; 
  } 
 
  getValue(): number { 
    return this.testvalue; 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

单例与实例化应用:

import ClassA from './A'; 
import ClassB from './B'; 
import ClassC from './C'; 
import { ClassD, ClassDD } from './D'; 
 
export class MSI11 { 
  List = [ClassA, ClassB, ClassC]; 
  d: ClassD = new ClassD(); 
  arr: ClassD[] = []; 
  arr2 = [new ClassD(), new ClassDD()]; 
 
  constructor() { 
  } 
 
 
  init(flag: number) { 
 
    this.List.map(ClassItem => { 
      // 此处我们需要new才能保证容器A和B调到的实例不是同一个 
      ClassItem.onRegister(); 
      if (flag == 1) { 
        ClassItem.setValue(10); 
        this.d.setValue(10); 
      } else { 
        ClassItem.setValue(20); 
        this.d.setValue(20); 
      } 
    }) 
    if (flag == 1) { 
      let testd: ClassD = new ClassD(); 
      testd.setValue(30); 
      this.arr.push(testd); 
      this.arr2[0].setValue(50); 
      this.arr2[1].setValue(60); 
    } else { 
      let teste: ClassD = new ClassD(); 
      teste.setValue(40); 
      this.arr.push(teste); 
      this.arr2[0].setValue(70); 
      this.arr2[1].setValue(80); 
    } 
    this.List.push(this.d); 
    this.arr.push(this.d); 
  } 
 
  getvalue(): number { 
    return this.arr[0].getValue(); 
  } 
 
  getvalue2(): number { 
    return this.arr[1].getValue(); 
  } 
 
  getvalue3(): number { 
    return this.arr2[0].getValue(); 
  } 
 
  getvalue4(): number { 
    return this.arr2[1].getValue(); 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.

调用处理:

import ClassA from './A'; 
import ClassB from './B'; 
import ClassC from './C'; 
import { MSI11 } from './msi'; 
 
const List = [ClassA, ClassB, ClassC]; 
 
// 容器A 
ClassA.onRegister(); 
ClassB.onRegister(); 
 
let a = new MSI11(); 
let b = new MSI11(); 
 
a.init(1); 
b.init(2); 
 
@Entry 
@Component 
 
struct Index { 
  @State message: string = 'Hello World'; 
  @State message2: string = 'Hello World2' 
  @State message3: string = 'Hello World3'; 
  @State message4: string = 'Hello World4'; 
  @State message5: string = 'Hello World5'; 
  @State message6: string = 'Hello World6'; 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(()=>{ 
            this.message = a.getvalue().toString() 
          }); 
        Text(this.message2) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(()=>{ 
            this.message2 = b.getvalue().toString() 
          }); 
        Text(this.message3) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(()=>{ 
            this.message3 = b.getvalue2().toString() 
          }); 
        Text(this.message4) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(()=>{ 
            this.message4 = b.getvalue2().toString() 
          }); 
        Text(this.message5) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(()=>{ 
            this.message5 = b.getvalue3().toString() 
          }); 
        Text(this.message6) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(()=>{ 
            this.message6 = b.getvalue4().toString() 
          }); 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.

实现效果

系统启动:

点击后:

分享
微博
QQ
微信
回复
2024-05-27 22:48:21
相关问题
首选项获取实例实例是否为
2623浏览 • 1回复 待解决
HarmonyOS Native 实例 ArkTS 对象
1057浏览 • 1回复 待解决
HarmonyOS 类被反复初始的问题
837浏览 • 1回复 待解决
HarmonyOS 问题
1141浏览 • 1回复 待解决
HarmonyOS 关于问题
1279浏览 • 1回复 待解决
HarmonyOS 对象如何实现
1338浏览 • 1回复 待解决
如何获取为undefined
1305浏览 • 1回复 待解决
HarmonyOS 模式不生效
997浏览 • 1回复 待解决
HarmonyOS TaskPool子线程问题
1143浏览 • 1回复 待解决
HarmonyOS静态库是否是
718浏览 • 1回复 待解决
HarmonyOS navigation有模式吗
949浏览 • 1回复 待解决
鸿蒙next 模式如何实现
368浏览 • 1回复 待解决
HarmonyOS 如何创建的WebView组件
794浏览 • 1回复 待解决
实现模式下的数据存储
2137浏览 • 1回复 待解决
HarmonyOS 路由跳转如何实现效果
881浏览 • 1回复 待解决
HarmonyOS 如何构建跨动态库的
1125浏览 • 1回复 待解决
提问
该提问已有0人参与 ,帮助了0人