如何在其成员函数kotlin中调用构造函数?

How to call a constructor within its member function kotlin?

根据 Microsoft 文档,我想将以下代码从 Java 转换为 Kotlin。但是,Kotlin 有 init 和构造函数,我不太确定如何在我的代码中实现它。这是因为我需要在其成员函数之一中调用构造函数class。

public class AzureServiceAdapter {
private String mMobileBackendUrl = "https://myappname.azurewebsites.net";
private Context mContext;
private MobileServiceClient mClient;
private static AzureServiceAdapter mInstance = null;

private AzureServiceAdapter(Context context) {
    mContext = context;
    mClient = new MobileServiceClient(mMobileBackendUrl, mContext);
}

public static void Initialize(Context context) {
    if (mInstance == null) {
        mInstance = new AzureServiceAdapter(context);
    } else {
        throw new IllegalStateException("AzureServiceAdapter is already initialized");
    }
}

以上代码是AndroidJava微软的代码。

参考下面的代码,我试图将 java 代码转换为 kotlin。但是,当我尝试花哨并将 init and constructor 添加到我的 kotlin 代码中时,我意识到我会在 mInstance!!.AzureServiceAdapter(context) ?: throw IllegalStateException("AzureServiceAdapter is already initialised")

处出错
class AzureServiceAdapter{
private val mMobileBackendUrl = "https://test123.azurewebsites.net"
private lateinit var mContext: Context
private lateinit var azureClient: MobileServiceClient
var mInstance : AzureServiceAdapter? = null


private fun AzureServiceAdapter(context: Context){
    mContext = context
    azureClient = MobileServiceClient(mMobileBackendUrl, mContext)
}


fun initialize(context: Context) {
    mInstance!!.AzureServiceAdapter(context) ?: throw IllegalStateException("AzureServiceAdapter is already initialised")
}

}

因此,有没有办法将Java代码转换为专业结构中的kotlin?

for Convert java file to kotlin

打开 Android Studio->打开您的项目->Select 您的 java 文件然后 继续 Navigation Bar->Code -> Convert java file to kotlin file

For this make sure your project is integrate with kotlin plugins