Reputation: 67
Whilst converting a rather simple application from Java to Kotlin
I came across one 'problem' I can't seem to fix.
I have a class ScanInfo
that looks like this in Java (without getters and setters)
public class ScanInfo {
private String source;
private String label_type;
private String data;
public ScanInfo(Intent intent) {
... Get Info from intent and assign the values ...
this.source = ...
this.label_type = ....
this.data = ....
}
}
Now in Kotlin
i can create the class
class ScanInfo (var source: String, var label_type: String, var data: String)
But I have no idea how to get it to work so I can create a ScanInfo
object with Intent as parameter.
I tried with companion, object, companion object but I can't seem to find the right syntax.
Am I wrong to look for such a solution while using Kotlin
or am I just not using the right kotlin-constructor
?
If it's possible, how can I create a ScanInfo
object with an Intent as parameter?
var result = ScanInfo(intent)
Thanks in advance.
Upvotes: 1
Views: 469
Reputation: 11963
That's the way:
class ScanInfo(intent: Intent) {
private val source = intent.source
private val labelType = intent.labelType
private val data = intent.data
}
OR
class ScanInfo(intent: Intent) {
private val source: String
private val labelType: String
private val data: String
init {
source = intent.data
labelType = intent.data
data = intent.data
}
}
Upvotes: 2