简体   繁体   中英

How to translate Java construtor to Kotlin properly?

I have a Java class which extends AsyncTask like this:

class PostLocationTask extends AsyncTask<String, Void, String> {
    private String responseServer;
    private double latitude, longitude

    public PostLocationTask(double latitude, double longitude){
        this.latitude = latitude;
        this.longitude = longitude;
    }

}

My goal is to translate the code into Kotlin. Now I have this:

 companion object {
        class PostLocationTask internal constructor (context: PostLocationActivity): AsyncTask<String, Void, String?>(){

            private var responseServer = ""
            private var lat = 0.0
            private var lon = 0.0

            class PostLocationTask(var currentLat:Double, var currentLon:Double){
                lat = currentLat
                lon = currentLon
            }
        }
}

Android Studio red-underlines those 2 lines inside the constructor, saying "Expecting member declaration". Wrapping those 2 lines inside "init {}" block will change the error message into "Unresolved reference: lat", for example.

What's the correct translation, then?

Do this.

import android.os.AsyncTask
import android.os.Build
import android.support.annotation.RequiresApi

@RequiresApi(Build.VERSION_CODES.CUPCAKE)
internal class PostLocationTask(
    private val latitude: Double, 
    private val longitude: Double) : AsyncTask<String, Void, String>() {
        
        private val responseServer: String? = null

        override fun doInBackground(vararg strings: String): String? {
            return null
    }
}

I hope it helps

I think so

class PostLocationTask 
constructor(var latitude: Double, var longitude: Double) : AsyncTask<String, Void, String>() {
    private var responseServer: String? = null

}

Kotlin hastwo types of constructors . First one is that goes after class name and second is that contains body of class and is declared as 'constructor'

In your case I'd make something like

class PostLocationTask(
    var latitude: Double? = null, 
    var longitude: Double? = null
) : AsyncTask<String, Void, String>() {
    var responseServer: String? = null
    override fun doInBackground(vararg params: String?): String = getLocationFromServer()
}

Solution: Here is the correct translation code in Kotlin.

inner class PostLocationTask(latitude: Double, longitude: Double) : AsyncTask<String, Void, String>() {
    private var responseServer: String? = null
    private var latitude: Double = latitude
    private var longitude: Double = longitude

    override fun doInBackground(vararg strings: String): String? {
        return null
    }
}

A shorter version

inner class PostLocationTask(private var latitude: Double, private var longitude: Double) : AsyncTask<String, Void, String>() {
    private var responseServer: String? = null

    override fun doInBackground(vararg strings: String): String? {
        return null
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM