Kotlin多个构造函数错误:声明具有相同的Java签名

问题描述 投票:0回答:1

我在FirebaseUserAuth类中有两个构造函数,并且给出错误:声明具有相同的Java签名。构造函数的参数不同。为什么会出现相同的签名错误?该如何解决?。

class FirebaseUserAuth(private val auth: FirebaseAuth,
                       private val loginCallBack: ((LoginAuthData) -> Unit)?,
                       private val registerVM_CallBack: ((RegisterAuthData) -> Unit)?)
{

  constructor(auth: FirebaseAuth, loginCallBack: (LoginAuthData) -> Unit): this(auth, loginCallBack, null)
  constructor(auth: FirebaseAuth, registerCallBack: (RegisterAuthData) -> Unit): this(auth, null, registerCallBack)
}

错误

> Platform declaration clash:The following declarations have the same JVM
> signature (Lcom/google/fireBase/auth/FirebaseAuth;Lkotlin/jvm/
> functions/Function1;)V):
> 
>  • public constructor FirebaseUserAuth(auth: FirebaseAuth,
> loginCallBack: (LoginAuthData) -> Unit) defined.
> com.examgle.data.FirebaseUserAuth 
> 
>  • public constructor FirebaseUserAuth(auth: FirebaseAuth,
> registerCallBack: (RegisterAuthData) -> Unit) defined.
> com.examgle.data.FirebaseUserAuth

android kotlin higher-order-functions constructor-overloading multiple-constructors
1个回答
3
投票
constructor(auth: FirebaseAuth, loginCallBack: (LoginAuthData) -> Unit)
constructor(auth: FirebaseAuth, registerCallBack: (RegisterAuthData) -> Unit)

两个构造函数都与JAVA中的函数参数的类型为Lkotlin/jvm/functions/Function1相同。

通过以下方式进行检查:

  1. Menu > Tools > Kotlin > Show Kotlin Bytecode
  2. 单击Decompile按钮
  3. 检查Java代码

我的建议是,如果可能,使用default参数而不是辅助构造函数:

class FirebaseUserAuth(private val auth: FirebaseAuth,
                       private val loginCallBack: ((LoginAuthData) -> Unit)? = null,
                       private val registerVM_CallBack: ((RegisterAuthData) -> Unit)? = null)
{
    ...
}
© www.soinside.com 2019 - 2024. All rights reserved.