kotlin set get 入门

kotlin set get 入门_第1张图片

var age: Int = 0
    get() = field
    set(value) {
        if (value > 100 || value < 0) field = -1
        else field = value
    }

var name: String = "tom"
    get() = field
    set(value) {
        if (value == null) {
            field = "";
        } else {
            field = value;
        }
    }

关于get set

get 返回的是field关键字

set 设置的是value关键字

你可能感兴趣的:(Kotlin)