Kotlin中的“out”關鍵字是什麼?
“Out”關鍵字廣泛使用於 Kotlin 泛型。它的簽名如下所示 −
List<out T>
當類 C 的型別引數 T 被宣告為 out,則 C
示例
以下示例演示瞭如何在 Kotlin 中使用“out”關鍵字 −
fun main(args: Array<String>) {
var objet1 = genericsExample<Int>(10)
var objet2 = genericsExample<Double>(10.0)
}
// As generic type is declared as "out",
// we can pass Int and Double also.
class genericsExample<out T>(input:Any?) {
init {
println("I am getting called with the value "+input)
}
}輸出
它將產生以下輸出
I am getting called with the value 10 I am getting called with the value 10.0
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP