簡體   English   中英

kotlin:專業構造函數中的“未解決的引用”

[英]kotlin: “unresolved reference” in sceondary constructor

我對Kotlin還是很陌生,也許這可能是一個愚蠢的問題,但是當我嘗試打印它時,為什么使用在第二個參數中定義的變量會產生“未解析的引用”錯誤,而在主構造函數中卻沒有發生

fun main(args: Array<String>){    
    var stud= Student("Yash", 10)    
}

class Student(name: String) {
    init {
        println("name is $name")
    }

    constructor(n: String, Id: Int): this(n) {
       println("name is $n")
       println("id is $id")
    }
}

參數id是小寫字母,但是您嘗試打印大寫字母ID,這樣就可以更改其工作了

構造函數(n:字符串,id:整數)

fun main(args: Array<String>){
var stud= Student("Yash", 10)
}

class Student(name: String) {
init {
    println("name is $name")
}

constructor(n: String, id: Int): this(n) {
    println("name is $n")
    println("id is $id")
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM