gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:25:39 28 4
gpt4 key购买 nike

如何在 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)
}

截图:/image/riGkU.png

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

我尝试创建构造函数。但是,我得到“委托(delegate)调用链中有一个循环”请更正我的代码并告诉我解决方案。 . .

最佳答案

对于数据类,类头中定义的构造函数是主构造函数。它不能委托(delegate)给该类中的任何其他构造函数(尽管它可以委托(delegate)给父类(super class)构造函数)。

但是,您可以在类的主体中定义辅助构造函数,只要这些构造函数委托(delegate)给主构造函数即可。例如:

data class EventItem(val dateEvent: String) {

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

您发布的代码中的问题是您的辅助构造函数试图委托(delegate)回自身。您必须改为委托(delegate)给主构造函数,这意味着您需要能够确定辅助构造函数中缺少的所有参数的值。

关于android - 如何在 Kotlin Android 中为数据类创建构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59201157/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com