Kotlin 抽象 class 二级构造函数

Kotlin abstract class secondary constructor

我无法为此抽象创建辅助构造函数 class。

@SuppressLint("StaticFieldLeak")
abstract class BaseAndroidViewModel(
        application: Application,
        private val creditsGetByIdUseCase: CreditsGetByIdUseCase,
        private val videosGetByIdUseCase: VideosGetByIdUseCase,
        private val reviewGetByIdUseCase: ReviewGetByIdUseCase,
        private val addToFavoritesUseCase: AddToFavoritesUseCase,
        private val getFavoriteByIdUseCase: GetFavoriteByIdUseCase
) : AndroidViewModel(application) {

constructor(application: Application) : this(application) // There's a cycle in the delegation calls chain error

当你创建一个 secondary 构造函数,并且 class 具有非空 primary 构造函数时,你应该传递所有参数primary 构造函数有,例如:

abstract class BaseAndroidViewModel(
        application: Application,
        private val creditsGetByIdUseCase: String,
        private val videosGetByIdUseCase: String
) : AndroidViewModel(application) {

    constructor(application: Application) : this(application, 
            "creditsGetByIdUseCase", "videosGetByIdUseCase") // here we pass other necessary parameters
}

在你的情况下可能是这样的:

@SuppressLint("StaticFieldLeak")
abstract class BaseAndroidViewModel(
    application: Application,
    private val creditsGetByIdUseCase: CreditsGetByIdUseCase?,
    private val videosGetByIdUseCase: VideosGetByIdUseCase?,
    private val reviewGetByIdUseCase: ReviewGetByIdUseCase?,
    private val addToFavoritesUseCase: AddToFavoritesUseCase?,
    private val getFavoriteByIdUseCase: GetFavoriteByIdUseCase?
) : AndroidViewModel(application) {

    constructor(application: Application) : this(application, null, null, null, null, null)
}

或者您可以使用默认参数创建构造函数:

abstract class BaseAndroidViewModel(
        application: Application,
        private val creditsGetByIdUseCase: CreditsGetByIdUseCase? = null,
        private val videosGetByIdUseCase: VideosGetByIdUseCase? = null,
        private val reviewGetByIdUseCase: ReviewGetByIdUseCase? = null ,
        private val addToFavoritesUseCase: AddToFavoritesUseCase? = null,
        private val getFavoriteByIdUseCase: GetFavoriteByIdUseCase? = null
    ) : AndroidViewModel(application) { ... }

你可以这样调用二级构造函数-

@SuppressLint("StaticFieldLeak") abstract class BaseAndroidViewModel : AndroidViewModel{
constructor(application: Application,
            creditsGetByIdUseCase: CreditsGetByIdUseCase,
            videosGetByIdUseCase: VideosGetByIdUseCase,
            reviewGetByIdUseCase: ReviewGetByIdUseCase,
            addToFavoritesUseCase: AddToFavoritesUseCase,
            getFavoriteByIdUseCase: GetFavoriteByIdUseCase) : super(application)

constructor(application: Application) : super(application) }

您获得 cycle in the delegation calls chain error 的原因是因为

constructor(application: Application): this(application)

相当于java:

public MyClass(Application application){
    this(application)
}

这表示您正在递归调用构造函数。


所述,您应该改为调用超级构造函数。

constructor(application: Application): super(application)

这实际上相当于您的主要构造函数:

BaseAndroidViewModel(application: Application): AndroidViewModel(application)

您可以声明这只是将关键字 constructor 替换为 BaseAndroidViewModel,将 super 替换为 AndroidViewModel