ArkTS层实例化与单例介绍

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

HarmonyOS
2024-05-26 17:59:06
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zhuyanxi

使用的核心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();

实例化类定义:

export class ClassD { 
  testvalue: number = 1; 
 
  constructor() { 
  } 
 
  onRegister() { 
    this.testvalue = 2; 
  } 
 
  setValue(value: number) { 
    this.testvalue = value; 
  } 
 
  getValue(): number { 
    return this.testvalue; 
  } 
}

单例与实例化应用:

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(); 
  } 
}

调用处理:

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%') 
  } 
}

实现效果

系统启动:

点击后:

分享
微博
QQ
微信
回复
2024-05-27 22:48:21
相关问题
首选项获取实例实例是否为
576浏览 • 1回复 待解决
实现模式下的数据存储
403浏览 • 1回复 待解决
创建的换了页面后不生效问题
699浏览 • 1回复 待解决
关于文档中说har中不能用的疑惑
395浏览 • 1回复 待解决
C++ 如何获取操作 Arkts 实例
393浏览 • 1回复 待解决
ArkTS获取对象的所有方法
459浏览 • 1回复 待解决
ArkTS异步机制执行顺序
235浏览 • 1回复 待解决
ArkTSC++互相直接调用
469浏览 • 1回复 待解决
图片边框borderImage介绍
484浏览 • 1回复 待解决
NAPI介绍(九) - gdb调试
161浏览 • 1回复 待解决
应用文件分享功能介绍
472浏览 • 0回复 待解决
oceanbase支持节点副本部署吗?
2958浏览 • 1回复 待解决
提问
该提问已有0人参与 ,帮助了0人