简体   繁体   English

如何用不同的泛型类型实例化基类?

[英]How to instantiate base class with different generic types?

I am trying to create a 2nd constructor and call the parent class with a different generic type based on the constructor invocation. 我正在尝试创建第二个构造函数,并根据构造函数调用使用不同的泛型类型调用父类。 1 type is GroupTrackInfoDTO and the other one is TrackInfoDTO but I keep getting a compiling error 一种类型是GroupTrackInfoDTO,另一种类型是TrackInfoDTO,但我一直收到编译错误

Platform declaration clash: The following declarations have the same JVM signature 平台声明冲突:以下声明具有相同的JVM签名

My code: 我的代码:

class GetSettingsTask
: BizOperationTask {

private var mCallback: BizTaskCallback<TrackSettings>? = null

constructor(operation: BizOperation<GroupTrackInfoDTO>,  mCallback: BizTaskCallback<TrackSettings>) : super(operation) {
    this.mCallback = mCallback
}


constructor(o: BizOperation<TrackInfoDTO>, mCallback: BizTaskCallback<TrackSettings>) : super(o) {
    this.mCallback = mCallback
}

if I add a dummy parameter to one of the constructors it worked but no idea why 如果我向构造函数之一添加一个哑元参数,则可以正常工作,但不知道为什么

constructor(o: BizOperation<TrackInfoDTO>, mCallback: BizTaskCallback<TrackSettings>, i: Int = 0) : super(o) {
        this.mCallback = mCallback
    }

They are the same JVM signature. 它们是相同的JVM签名。 You can define an abstract class ( AbstractTrackInfoDTO ) that is the parent of TrackInfoDTO and GroupTrackInfoDTO then declare the constructor as below: 您可以定义一个抽象类( AbstractTrackInfoDTO ),它是TrackInfoDTOGroupTrackInfoDTO的父TrackInfoDTO ,然后声明构造函数,如下所示:

constructor(operation: BizOperation<AbstractTrackInfoDTO>,  mCallback: BizTaskCallback<TrackSettings>) : super(operation) {
    this.mCallback = mCallback
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM