2017-06-06 54 views
0

我有這個在Java中:如何將此Java類重寫爲Kotlin數據類?

public class User { 

    String name; 
    String id; 
    int age; 

    public User(String name) { 
     this.name = name; 
    } 

    public User(String name, String id) { 
     this.name = name; 
     this.id = id; 
    } 

    public User(String id, int age) { 
     this.id = id; 
     this.age = age; 
    } 

    public User() { 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public int getAge() { 
     return age; 
    } 

    public void setAge(int age) { 
     this.age = age; 
    } 

    @Override 
    public String toString() { 
     return "User{" + 
       "name='" + name + '\'' + 
       ", id='" + id + '\'' + 
       ", age=" + age + 
       '}'; 
    } 
} 

在科特林類公司是這樣的:

class User { 

    var name: String?=null 
    var id: String?=null 
    var age: Int = 0 

    constructor(name: String) { 
     this.name = name 
    } 

    constructor(name: String, id: String) { 
     this.name = name 
     this.id = id 
    } 

    constructor(id: String, age: Int) { 
     this.id = id 
     this.age = age 
    } 

    constructor() {} 

    override fun toString(): String { 
     return "User{" + 
       "name='" + name + '\'' + 
       ", id='" + id + '\'' + 
       ", age=" + age + 
       '}' 
    } 
} 

我需要知道什麼,是怎麼做的,在與4個不同的數據類構造函數以及setter和getters

回答

4

您可以使用默認參數和命名參數的組合,因此不需要多個構造函數。定義你的數據類是這樣的:

data class User(var name: String? = null, var age: Int = 0, var id: String? = null) 

,然後構造它的任何數量的方式。

User(name = "Foo") // returns User("Foo", 0, null) 
User(name = "Foo", age = 21) // returns User("Foo", 21, null) 
User(id = "x") // returns User(null, 0, "x") 
User("Foo", 21, "x") // returns User("Foo", 21, "x") 
User(id = "x", name = "Foo", age = 21) // returns User("Foo", 21, "x") 
etc... 

getters和setters是爲您定義的,不需要定義它們。

0

在科特林我們可以在這樣的

data class User(var name: String, var age: Int = 0, var id: String = "ABC") 

一個構造函數如果我們要公開getter和沒有setter方法,我們只是提供VAL,而不是VAR像下面,

data class User(val name: String, val age: Int = 0, val id: String = "ABC") 

getter/setter方法的可視性修飾符,我們可以爲他們提供在這樣的構造,

data class User(internal val name, internal val age: Int = 0, internal var id: String = "ABC") 

在上面這種情況下,getter只能訪問該模塊。

我們可以創建這樣的對象,

var user = User("XYZ") 
    var user1 = User("XYZ", 29) 
    var user2 = User("XYZ", 29, "AXCVFC") 
0

的Android Studio提供了一個非常簡單的方法來實現任何Java類轉換成科特林。只需鍵入「將Java到科特林」在文件中的搜索選項卡,IDE會爲你做 enter image description here

0

只是我加入不同的答案從其他的,因爲如果你有用戶數據類裏面太多變量,那麼它應該是太如下圖所示。

data class UserTooLongClass(var id: String, var name : String, var age : String, var location : String, var mobileNo : String, var mailId : String) 

,所以你可以創建類似下面,

data class User(var id: String = "1") { 
    var name: String = "" 
    var age: Int = 0 
    var location : String = "" 
    var mobileNo : String = "" 
    var mailId : String = "" 

    override fun toString(): String { 
     return " id : $id, name : $name, age : $age, location : $location, mobileNo : $mobileNo, mailId : $mailId" 
    } 
} 

實現:

val user : User = User() 
    user.name = "TestName" 
    user.id = "1223" 
    user.age = 23 
    user.mobileNo = "333" 
    user.mailId = "[email protected]" 
    user.location = "xxxxx" 
    Log.d("TAG"," user : "+user.toString()) 

輸出:

TAG: user : id : 1223, name : TestName, age : 23, location : xxxxx, mobileNo : 333, mailId : [email protected] 
相關問題