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

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

private currentRequst:BleRequest<?>;

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

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

阅读 541
1 个回答

参考示例如下:

@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)
  }
}
logo
HarmonyOS
子站问答
访问
宣传栏