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

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

private currentRequst:BleRequest<?>;

currentRequest  = new AuthRequest();
currentRequest  = new CommRequest();
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

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

HarmonyOS
2024-12-23 16:03:04
浏览
收藏 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)
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-23 17:27:41


相关问题
HarmonyOS属性如何初始化?
1359浏览 • 1回复 待解决
HarmonyOS如何获取一个名称
2016浏览 • 4回复 待解决
HarmonyOS 如何设置迭代器
1125浏览 • 2回复 待解决
HarmonyOS 可以被继承并重写吗
877浏览 • 1回复 待解决
ArkTS中继承机制是怎样?
1233浏览 • 1回复 待解决
如何在ArkTS中定义和使用?
1838浏览 • 1回复 待解决
HarmonyOS 组件如何添加
1045浏览 • 1回复 待解决