当您创建辅助构造函数并且类具有非空主构造函数时,您应该传递主构造函数具有的所有参数,例如:
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) { ... }