HarmonyOS 带泛型的类定义继承之后,子类如何赋值给父类

class Request<T> { }
class AuthRequest<string>{}
class CommRequest<number>{}

private currentRequst:BleRequest<?>;

currentRequest  = new AuthRequest();
currentRequest  = new CommRequest();

ts里面正常问号可以填写成any,ArkTS这样逻辑如何写?

HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
shlp

参考示例如下:

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  @State param: compA<string> | null = null;
  @State param2: comp2A<string> | null = null;

  aboutToAppear(): void {
    this.param = new compB("11", 1);
    this.param2 = new comp2B(() => {
    })

  }

  build() {
    Column({ space: 10 }) {
      Text(JSON.stringify(this.param))
      Text(JSON.stringify(this.param2))
    }
    .width("100%")
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }
}

class compA <T> {
  test: string

  constructor(test: string) {
    this.test = test;
  }
}

class compB extends compA<number> {
  test1: number;

  constructor(test: string, test1: number) {
    super(test);
    this.test1 = test1;
  }
}

class comp2A <T> {
  public onResult: (code: number, result: T) => void;

  constructor(onResult: (code: number, result: T) => void) {
    this.onResult = onResult;
  }
}

class comp2B extends comp2A<ESObject> {
  constructor(onResult: (code: number, result: number) => void) {
    super(onResult)
  }
}
分享
微博
QQ
微信
回复
3天前
相关问题
HarmonyOS属性如何初始化?
480浏览 • 1回复 待解决
HarmonyOS如何获取一个名称
903浏览 • 4回复 待解决
HarmonyOS 如何设置迭代器
51浏览 • 2回复 待解决
HarmonyOS 可以被继承并重写吗
42浏览 • 1回复 待解决
如何在ArkTS中定义和使用?
376浏览 • 1回复 待解决
ArkTS中继承机制是怎样?
178浏览 • 1回复 待解决
HarmonyOS 组件如何添加
32浏览 • 1回复 待解决
HarmonyOS 类型判断
44浏览 • 1回复 待解决