简体   繁体   English

如何在 Kotlin Android 中为数据类创建构造函数?

[英]How to create constructor for data class in Kotlin Android?

How to create constructor for data class in Kotlin Android ?如何在 Kotlin Android 中为数据类创建构造函数?

data class EventItem(
        @SerializedName("dateEvent")
        val dateEvent: String,
        @SerializedName("dateEventLocal")
        val dateEventLocal: Any,
        @SerializedName("idAwayTeam")
        val idAwayTeam: String,
        @SerializedName("idEvent")
        val idEvent: String,
        @SerializedName("idHomeTeam")
        val idHomeTeam: String,
        @SerializedName("idLeague")
        val idLeague: String,
        @SerializedName("idSoccerXML")
        val idSoccerXML: String,
        @SerializedName("intAwayScore")
        val intAwayScore: Any,
        @SerializedName("intAwayShots")
        val intAwayShots: Any,
        @SerializedName("intHomeScore")
        val intHomeScore: Any,
        @SerializedName("intHomeShots")
        val intHomeShots: Any,
        @SerializedName("intRound")
        val intRound: String,
        @SerializedName("intSpectators")
        val intSpectators: Any,
        @SerializedName("strAwayFormation")
        val strAwayFormation: Any,
        @SerializedName("strAwayGoalDetails")
        val strAwayGoalDetails: String,
        @SerializedName("strAwayLineupDefense")
        val strAwayLineupDefense: String,
        @SerializedName("strAwayLineupForward")
        val strAwayLineupForward: String,
        @SerializedName("strAwayLineupGoalkeeper")
        val strAwayLineupGoalkeeper: String,
        @SerializedName("strAwayLineupMidfield")
        val strAwayLineupMidfield: String,
        @SerializedName("strAwayLineupSubstitutes")
        val strAwayLineupSubstitutes: String,
        @SerializedName("strAwayRedCards")
        val strAwayRedCards: String,
        @SerializedName("strAwayTeam")
        val strAwayTeam: String,
        @SerializedName("strAwayYellowCards")
        val strAwayYellowCards: String,
        @SerializedName("strBanner")
        val strBanner: Any,
        @SerializedName("strCircuit")
        val strCircuit: Any,
        @SerializedName("strCity")
        val strCity: Any,
        @SerializedName("strCountry")
        val strCountry: Any,
        @SerializedName("strDate")
        val strDate: String,
        @SerializedName("strDescriptionEN")
        val strDescriptionEN: Any,
        @SerializedName("strEvent")
        val strEvent: String,
        @SerializedName("strEventAlternate")
        val strEventAlternate: String,
        @SerializedName("strFanart")
        val strFanart: Any,
        @SerializedName("strFilename")
        val strFilename: String,
        @SerializedName("strHomeFormation")
        val strHomeFormation: Any,
        @SerializedName("strHomeGoalDetails")
        val strHomeGoalDetails: String,
        @SerializedName("strHomeLineupDefense")
        val strHomeLineupDefense: String,
        @SerializedName("strHomeLineupForward")
        val strHomeLineupForward: String,
        @SerializedName("strHomeLineupGoalkeeper")
        val strHomeLineupGoalkeeper: String,
        @SerializedName("strHomeLineupMidfield")
        val strHomeLineupMidfield: String,
        @SerializedName("strHomeLineupSubstitutes")
        val strHomeLineupSubstitutes: String,
        @SerializedName("strHomeRedCards")
        val strHomeRedCards: String,
        @SerializedName("strHomeTeam")
        val strHomeTeam: String,
        @SerializedName("strHomeYellowCards")
        val strHomeYellowCards: String,
        @SerializedName("strLeague")
        val strLeague: String,
        @SerializedName("strLocked")
        val strLocked: String,
        @SerializedName("strMap")
        val strMap: Any,
        @SerializedName("strPoster")
        val strPoster: Any,
        @SerializedName("strResult")
        val strResult: Any,
        @SerializedName("strSeason")
        val strSeason: String,
        @SerializedName("strSport")
        val strSport: String,
        @SerializedName("strTVStation")
        val strTVStation: Any,
        @SerializedName("strThumb")
        val strThumb: Any,
        @SerializedName("strTime")
        val strTime: String,
        @SerializedName("strTimeLocal")
        val strTimeLocal: String,
        @SerializedName("strTweet1")
        val strTweet1: Any,
        @SerializedName("strTweet2")
        val strTweet2: Any,
        @SerializedName("strTweet3")
        val strTweet3: Any,
        @SerializedName("strVideo")
        val strVideo: Any
    ) {
        constructor(
            idEvent: String,
            strEvent: String,
            strDate: String,
            idHomeTeam: String,
            strHomeTeam: String,
            intHomeScore: Any,
            idAwayTeam: String,
            strAwayTeam: String,
            intAwayScore: Any
            ) : this(idEvent, strEvent, strDate, idHomeTeam, strHomeTeam,  intHomeScore, idAwayTeam, strAwayTeam, intAwayScore)
    }

screenshot : https://i.stack.imgur.com/riGkU.png截图: https : //i.stack.imgur.com/riGkU.png

How to create constructor for data class in Kotlin Android ?如何在 Kotlin Android 中为数据类创建构造函数?

i've try to create constructor.我尝试创建构造函数。 But, i get "There's a cycle in the delegation calls chain" please, correct my code and tell me solution for it.但是,我得到“委托调用链中有一个循环”,请更正我的代码并告诉我解决方案。 . . . .

For a data class, the constructor defined in the class header is the primary constructor .对于数据类,类头中定义的构造函数是主构造函数 It can't delegate to any other constructor in that class (though it can delegate to a superclass constructor).它不能委托给该类中的任何其他构造函数(尽管它可以委托给超类构造函数)。

However, you can define secondary constructors in the body of the class, as long as those delegate to the primary constructor.但是,您可以在类的主体中定义辅助构造函数,只要它们委托给主构造函数即可。 For example:例如:

data class EventItem(val dateEvent: String) {

    constructor(date: Date) : this(date.toString())
}

The problem in the code you've posted is that your secondary constructor is trying to delegate back to itself.您发布的代码中的问题是您的辅助构造函数正试图委托给自己。 You have to delegate to the primary constructor instead, and that means you need to be able to determine a value for all of the parameters missing from the secondary constructor.您必须改为委托给主构造函数,这意味着您需要能够确定辅助构造函数中缺少的所有参数的值。

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

相关问题 如何在 Kotlin Android 中为数据类创建空构造函数 - How to create empty constructor for data class in Kotlin Android 如何使用kotlin创建在构造函数中传递方法引用的泛型类的实例 - How to create instance of generic class passing a method reference in constructor with kotlin 如何为这个网络服务创建 kotlin 数据类? - How to create kotlin data class for this network service? Android - 在 kotlin 中重载 Application class 构造函数是否正确? - Android - Is it correct to overload the Application class constructor in kotlin? 反序列化Kotlin数据类Android - Deserialization kotlin data class android 如何使用Kotlin在Android中为自定义视图创建基类? - How to create base class for custom views in Android using Kotlin? 如何在 android kotlin 中创建合并适配器 class - How can create merge adapter class in android kotlin 在Kotlin中,带有匿名类的构造函数如何工作? - In Kotlin, how does a constructor with anonymous class work? 如何使用 Kotlin 创建自定义视图的构造函数 - How to create constructor of custom view with Kotlin 构造函数中的参数是什么(上下文,属性)? 我如何创建新的 object 这个 class? Java 和 Kotlin - What is parameters in constructor (context, attrs)? How I can create new object this class? Java and Kotlin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM