如何让kotlin中的二级构造函数[重复]。

问题描述 投票:0回答:1

在Kotlin中,这是我写的代码,但在二级构造函数this()处显示错误。

class MyCustomAdapter(context: Context, countryModelsList:ArrayList<CountryModel>) :
    ArrayAdapter<CountryModel>(context,R.layout.list_coustom_item,countryModelsList ) {
    constructor() :  this()

错误是 "有周期性的调用链"

java android android-studio kotlin kotlin-android-extensions
1个回答
0
投票

正如tynn所建议的 此处,

如果这个类有一个主构造函数,那么每个次要构造函数需要直接或间接地通过另一个次要构造函数委托给主构造函数。委托给同一类的另一个构造函数是通过this关键字来完成的。

我无法理解你想解决的具体用例,但我想下面的代码会有帮助。

class MyCustomAdapter(context: Context, resourceId: Int, countryModelsList:ArrayList<CountryModel>) :
ArrayAdapter<CountryModel>(context, resourceId, countryModelsList ) {

constructor(context: Context, countryModelsList: ArrayList<CountryModel>) : this(context, R.layout.list_coustom_item, countryModelsList)

constructor(context: Context): this(context, emptyList<CountryModel>())

}

© www.soinside.com 2019 - 2024. All rights reserved.