代码之家  ›  专栏  ›  技术社区  ›  Amrmsmb

如何打印getter方法的值

  •  0
  • Amrmsmb  · 技术社区  · 8 年前

    我在eclipse中创建了下面的示例。我遵循一个教程,它提到我可以使用 print(s1.captain) 例如打印船长的名字。本教程想说明这一点 kotlin 自动生成 setters 和 getters

    在我的代码中,print语句不打印任何东西

    :

    fun main(args : Array<String>) {
        var s1 = Stronghold1("JJ",7)
        print(s1.captain)
    }
    

    :

    abstract class Stronghold(name: String, location: String)
    

    据点1

    class Stronghold1(captain: String, capacity: Int) : Stronghold("GerMachine", "Bonn")
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   Todd    8 年前

    在Kotlin中,只有将构造函数参数标记为 val 或 var captain , capacity , name location 只是构造函数的参数。它们不是用来做财产的。

    得到 和 容量 瓦尔 致他们:

    class Stronghold1(val captain: String, val capacity: Int) : Stronghold("GerMachine", "Bonn")
    //                ^^^                  ^^^
    //                add                  add   
    

    你可能想用 Stronghold

    abstract class Stronghold(val name: String, val location: String)
    //                        ^^^               ^^^
    //                        add               add