我从一个api调用通过改造得到以下模型:
data class User(
@Expose val id: Int
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?,
@Expose var payments: List<PaymentMethod>
)
我需要添加一个额外的属性,原因是
this
所以回答。
额外的参数将是数据类paymentsmethods:
data class PaymentsMethods(
val paymentMethods: List<PaymentMethod>
)
如何在创建属性值时将其添加到用户类?
以下是我迄今为止所做的尝试:
// add an attribute (with a default value) that won't be filled by the parser.
data class User(
@Expose val id: Int
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?,
@Expose var payments: List<PaymentMethod>,
@Expose var paymentsObject: PaymentMethods = PaymentMethods(payments)
)
和
// create variable and set the value on init
data class User(
@Expose val id: Int
@Expose var username: String,
@Expose var phoneNum: String?,
@Expose var email: String?,
@Expose var payments: List<PaymentMethod>
){
init {
var paymentsObject: PaymentMethods = PaymentMethods(payments)
}
}
它们都不起作用。任何建议都将不胜感激。