我想运行下面提到的简单示例。eclipse 生成错误 说:
main class cant be found or loaded
请让我知道如何修复此错误以及为什么它发生在我尝试使用支持字段的以下代码中。但是,它们在代码中的使用方式并没有提供预期的输出。请参阅输出部分。
如何显示支持字段的输出
代码:
fun main(args: Array<String>) {
println("Hello, World!")
val p1 = Person_1("jack", 21);
p1.lastName = "stephan"
p1.month = "may"
println("lastName is ${p1.getLastName}")
println("month is ${p1.getMonth}")
val p2 = Person_1("jack", 21);
p2.lastName = "knauth"
p2.month = "june"
println(p2.getLastName)
println(p2.getMonth)
class Person_1 (val name: String, val age : Int) {
//backing field 1
var lastName : String? = null
set(value) {
if (value?.length == 0) throw IllegalArgumentException("negative values are not allowed")
field = value
}
val getLastName
get() = {
lastName
}
//backing field 2
var month : String? = null
set(value) {
field = value
}
val getMonth
get() = {
month
}
}
输出:
Hello, World!
lastName is () -> kotlin.String?
month is () -> kotlin.String?
() -> kotlin.String?
() -> kotlin.String?