2017-05-31 39 views
16

我正在從官方文檔學習Kotlin,我創建了一個如下所示的class,我創建了一個constructor,它有兩個parametersconstructor正文在init區塊。Kotlin的搭建商

class Person(name: String, surname: String) { 
    init { 
     Log.d("App", "Hello"); 
    } 
} 

好吧,我想創建一個constructor一個更constructor這將需要一個parameterKotlin

回答

22

init不是構造體。它在主構造函數和主構造函數的上下文之後被調用。

作爲正式文件給出:

The primary constructor cannot contain any code. Initialization code can be placed in initializer blocks, which are prefixed with the init keyword:

class Customer(name: String) { 
    init { 
     logger.info("Customer initialized with value ${name}") 
    } 
} 

Note that parameters of the primary constructor can be used in the initializer blocks. They can also be used in property initializers declared in the class body:

class Customer(name: String) { 
    val customerKey = name.toUpperCase() 
} 

In fact, for declaring properties and initializing them from the primary constructor, Kotlin has a concise syntax:

class Person(val firstName: String, val lastName: String, var age: Int) { 
    // ... 
} 

根據你的問題,你可以添加一個構造函數接受一個參數如下:

class Person(name: String, surname: String) { 

    constructor(name: String) : this(name, "") { 
     // constructor body 
    } 

    init { 
     Log.d("App", "Hello"); 
    } 
} 

但它看起來不正確,因爲我們沒有必要傳遞第二個參數空字符串。所以我們可以訂購如下構造函數:

class Person(name: String) { 

    constructor(name: String, surname: String) : this(name) { 
     // constructor body 
    } 

    init { 
     Log.d("App", "Hello"); 
    } 
} 

希望它有幫助。

+0

'this(name)'調用主構造函數嗎?如果是的話,我們可以登錄檢查? –

+0

'this()'用於調用構造函數,其參數簽名定義調用哪個構造函數。 – chandil03

+0

是的正確。我們可以檢查是否通過日誌記錄調用主構造函數嗎? –

2

在裏面使用constructor關鍵字創建次要構造函數。像:

class Person(name: String, surname: String) { 
    init { 
     Log.d("App", "Hello"); 
    } 
    constructor(id: Int) { 

    } 
} 

欲瞭解更多信息檢查Secondary Constructors

編輯:
規則:如果類有一個主構造,每個次級構造需要委託給主要的構造函數,直接或通過另一個輔助構造函數間接進行。使用this關鍵字完成對同一類的另一個構造函數的委託。

class Person(val name: String) { 
    constructor(name: String, parent: Person) : this(name) { 
     parent.children.add(this) 
    } 
} 

所以,當你調用二次構造函數,它調用主構造函數初始化的名字之後,你做你的東西在二級構造。在上面的例子中,通過調用主構造函數初始化該名稱。

+0

我有這樣的代碼'類人(名稱:字符串,姓:字符串){ 初始化{ Log.d( 「你好」, 「你好$ $名字姓」); } 構造函數(參數name:String){ } }'加入二級構造函數之後,Android Studio中暗示我'錯誤:(10,5)主構造函數調用expected' –

+0

這是因爲,你將不得不調用使用此關鍵字的主構造函數。儘快寫一個答案來演示。 –

1

二級構造

類也可以聲明次級構造,其前綴構造:

class Person { 
    constructor(parent: Person) { 
     parent.children.add(this) 
    } } 

如果類具有主構造中,每個次級構造需要委託給主構造,直接或間接通過另一個輔助構造函數。代表團到同一類的其他構造函數中使用this關鍵字做到:

class Person(val name: String) { 
    constructor(name: String, parent: Person) : this(name) { 
     parent.children.add(this) 
    } } 

看到https://kotlinlang.org/docs/reference/classes.html二級構造函數部分

1

這就是你如何創建另一個構造函數。

class Person(name: String, surname: String) { 
    init { 
     Log.d("App", "Hello"); 
    } 
constructor(id: Int) : this("example name", "example surname") { 

} 
} 

一定要記住,二級構造函數必須使用this關鍵字引用主構造函數及其參數。

2

空值

// (name: String, surname: String) default constructor signature 
class Person(name: String, surname: String) { 

    // init block , represents the body of default constructor 
    init { 
     Log.d("primary", "Hello"); 
    } 

    // secondary constructor 
    // this(name,"") call to default constructor 
    constructor(name : String):this(name,""){ 
     Log.d("secondary", "Hello"); 
    } 
} 

爲什麼this(name,"")

If the class has a primary constructor, each secondary constructor needs to delegate to the primary constructor, either directly or indirectly through another secondary constructor(s). Delegation to another constructor of the same class is done using the this keyword:

科特林將不允許使用nullthis(name,null)所以使用?表示與類型null值第一種方式, surname: String?

class Person(name: String, surname: String?) { 

    init { 
     Log.d("primary", "Hello"); 
    } 

    constructor(name : String):this(name,null){ 
     Log.d("secondary", "Hello"); 
    } 
}