当我将kotlin变量设置为私有时,我收到一条错误消息,说“修改器私有不适用于局部变量

时间:2018-08-21 15:16:42

标签: java kotlin

class Bank_ {
    fun main(args: Array<String>) {

        private val bank_name:String                 
        private val bank_country:String
        private val ank_address:String
        private val bank_code:Int
}}

1 个答案:

答案 0 :(得分:1)

那是因为这是真的。您不能在方法内部将变量声明为private

但是,您可以在这样的课堂上这样做:

class Bank { 
    private val name: String = ...                 
    private val country: String = ...
    private val address: String = ...
    private val code: Int = ...
}

或者您宁愿使用数据类:

data class Bank(
    val name: String, 
    val country: String,
    val address: String,
    val code: Int
)

...或这样的方法级别:

fun main(args: Array<String>) {
    val name: String                 
    val country: String
    val address: String
    val code: Int
}