为什么我的代码无法访问?

时间:2016-03-19 07:25:51

标签: kotlin

我是Kotlin的新手,并试图写一个家谱程序,但我收到了一个错误:

Warning:(30, 14) Kotlin: Parameter 'args' is never used
Warning:(32, 9) Kotlin: Unreachable code

我该如何解决?

data class Person(
        val id: Int,
        val name: String
) {

    fun main(args: Array<String>) {
        val person = Person(1, "Nikolay")
        println("my name is ${person.name}")
    }
}

我无法弄清楚如何让它发挥作用。

哇,它有效!

data class Person(
        val id: Int,
        val name: String,
        val gender: Int,
        val father: Int,
        val mother: Int,
        val children: List<Int>,
        val email: String,
        val homepage: String,
        val date_of_birth: String,
        val date_of_death: String,
        val is_living: Boolean,
        val place_of_birth: String,
        val place_of_death: String,
        val cemetery: String,
        val schools: List<String>,
        val jobs: List<String>,
        val work_places: String,
        val places_of_living: String,
        val general: String,
        val default_picture: String
)

fun main(args: Array<String>) {
     val person =Person(1, "Nikolay", 1, 10, 11, listOf(2), "mi@ya.ru", 
                        "mishin.narod.ru", "07.02.1976",
                        "", true, "Moscow", "", "", listOf("780", "MIIT"), 
                        listOf("luxoft", "lotus domino"), 
                        "Nagornaya", "Buninskaya Alleya", "perl and kotlin programmer", "")
    println("my name is ${person.name}")
}

但也对日期时间提出质疑 怎么用呢? 它支持Kotlin吗?

1 个答案:

答案 0 :(得分:4)

如果要在main方法中创建Person的新实例,则需要将main函数从类中移出到包级别:

data class Person(
        val id: Int,
        val name: String
)

fun main(args: Array<String>) {
    val person = Person(1, "Nikolay")
    println("my name is ${person.name}")
}

现在你可以和Kotlin一起玩了。