[英]Android kotlin - secondary constructor
I want to implement a class with two constrcutors. 我想用两个构造函数实现一个类。 And empty constructor and another with a param
user:FirebaseUser
空的构造函数和另一个具有参数
user:FirebaseUser
But i'm getting the message error: 但是我收到消息错误:
"There is a cycle in the delegation change"
“代表团变更有一个周期”
class UserModel(user: FirebaseUser) {
var uid: String?
val email: String?
val phoneNumber: String?
val photoUrl: String
val displayName: String?
//error message: There is a cycle in the delegation change
constructor():this() {}
init {
this.displayName = user.displayName
this.email = user.email
this.phoneNumber = user.phoneNumber
this.photoUrl = user.photoUrl!!.toString()
this.uid = user.uid
}
companion object {
@Exclude
val CURRENT_LOCATION = "location"
}
}
I've tried several approaches without success. 我尝试了几种方法但均未成功。 Any help?
有什么帮助吗?
All the secondary constructors have to call the primary constructor, either directly or indirectly. 所有辅助构造函数都必须直接或间接调用主构造函数。 Which means:
意思是:
class X(var x: Int){
constructor() : this(0.0);
constructor(x: Double) : this(x.toInt());
}
However, you can't do this: 但是,您不能这样做:
class X(var x: Int){
constructor() : this();
constructor(x: Double) : this();
}
Because it would result in a Stack Overflow Exception. 因为这会导致堆栈溢出异常。
The above example is horrible code, but it's just as a demo. 上面的示例是可怕的代码,但这只是一个演示。
So with this line: 所以这行:
constructor():this() {}
You make the secondary constructor call itself. 您可以使辅助构造函数自行调用。
Instead, call the primary constructor. 而是调用主构造函数。 This means you need to pass a
FirebaseUser
as an argument. 这意味着您需要传递
FirebaseUser
作为参数。 I'm not familiar with Firebase, so I'll leave that to you. 我对Firebase并不熟悉,所以我将其留给您。
But as an example, you basically need to do this instead: 但是作为示例,您基本上需要这样做:
constructor() : this(FirebaseUser());
Either initialize directly, or get it from a method. 直接初始化,或从方法中获取它。 If you can't get one, you could of course just make it nullable.
如果您无法获得一个,则当然可以将其设置为可为空。
But if you're dealing with nullables, assuming the majority of your code is in Kotlin, you can just make it nullable with a default value and remove the secondary constructor: 但是,如果您要处理可空值,那么假设您的大多数代码都在Kotlin中,则可以使用默认值将其设置为可空值,然后删除辅助构造函数:
class UserModel(user: FirebaseUser? = null){
init{
// Now that `user` is nullable, you need to change the assignments to include null-safe or non-null assertion (?. or !!. respectively)
}
}
You have to call into the primary constructor from every secondary constructor you have, since its parameters may be used in property initializers and initializer blocks, like you've used user
in the init
block in your code. 您必须从拥有的每个辅助构造函数中调用主构造函数,因为它的参数可能在属性初始化程序和初始化程序块中使用,就像您在代码的
init
块中使用过user
一样。
With this code, the secondary constructor just recursively calls itself: 使用此代码,辅助构造函数只是递归地调用自身:
constructor() : this() {}
What you should do instead is call into the primary constructor so that the class' properties can be initialized: 相反,您应该做的是调用主构造函数,以便可以初始化类的属性:
constructor() : this(FirebaseUser()) {} // get FirebaseUser from somewhere
Alternatively, if what you meant to do is leave everything null
when the secondary no-param constructor is called, you could opt for something like this: 另外,如果您要执行的操作是在调用第二个无参数构造函数时将所有内容保留为
null
,则可以选择以下内容:
class UserModel(user: FirebaseUser?) {
var uid: String? = user?.uid
val email: String? = user?.email
val phoneNumber: String? = user?.phoneNumber
val photoUrl: String? = user?.photoUrl?.toString()
val displayName: String? = user?.displayName
constructor() : this(null)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.