[英]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)
}
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.