Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 我无法为受保护的属性编写getter和setter_Kotlin - Fatal编程技术网

Kotlin 我无法为受保护的属性编写getter和setter

Kotlin 我无法为受保护的属性编写getter和setter,kotlin,Kotlin,我刚刚开始学习Kotlin,以前学过Java 在Java中,我们可以为受保护的属性创建getter和setter。然而,在Kotlin中,我只能为私有属性添加getter和setter 如何为受保护的属性编写getter和setter?这应该可以: protected val blah: Int get() = 2 可以帮助你:你能提供你的代码并解释什么不起作用吗?我试过了,但当我试图从主函数获取这个属性时,我看不到这个attribute@PhucNguyen然后,您需要了解可见性修改

我刚刚开始学习Kotlin,以前学过Java

在Java中,我们可以为受保护的属性创建getter和setter。然而,在Kotlin中,我只能为私有属性添加getter和setter

如何为受保护的属性编写getter和setter?

这应该可以:

protected val blah: Int
    get() = 2

可以帮助你:你能提供你的代码并解释什么不起作用吗?我试过了,但当我试图从主函数获取这个属性时,我看不到这个attribute@PhucNguyen然后,您需要了解可见性修改器。无法从主功能访问受保护的
成员。只有子类才能访问受保护的成员。@TaseerAhmad谢谢你花时间,我现在可以明白了:D