kotlin set 私有

In this tutorial, we’ll be discussing the various Visibility Modifiers available in Kotlin programming.

在本教程中,我们将讨论Kotlin编程中可用的各种Visibility Modifiers。

Kotlin可见性修改器 (Kotlin Visibility Modifiers)

Visibility Modifiers are modifiers that when appended to a class/interface/property/function in Kotlin, would define where all it is visible and from where all it can be accessed.
The setters of properties in Kotlin can have a separate modifier from the property. The getters can’t have a visibility modifier defined. They use the same modifier as that of the property.

可见性修饰符是一种修饰符,当附加到Kotlin中的类/接口/属性/功能时,将定义所有可见位置以及可从何处访问所有内容。
Kotlin中的属性设置器可以具有与属性不同的修饰符。 吸气剂不能定义可见性修饰符。 它们使用与属性相同的修饰符。

Following are the visibility modifiers:

以下是可见性修改器:

  • public上市
  • protected受保护的
  • internal内部
  • private私人的

公共修饰符 (Public Modifier)

A Public Modifier is the default modifier in Kotlin. Just like the Java public modifier, it means that the declaration is visible everywhere.

公共修饰符是Kotlin中的默认修饰符。 就像Java公共修饰符一样,它意味着该声明在任何地方都可见。

class Hello{
}public class H{
}fun hi()
public fun hello()val i = 0
public val j = 5

All the above declarations are the in the top level of the file. ALL are public.
If we don’t mention the declaration of the members of the class, they are public(unless they are overridden).

以上所有声明都在文件的顶层。 所有都是公开的。
如果我们不提及类成员的声明,则它们是公共的(除非它们被重写)。

保护修饰符 (Protected Modifier)

A Protected Modifier in Kotlin:
CANNOT be set on top-level declarations.
Declarations that are protected in a class, can be accessed only in their subclasses.

Kotlin中的保护修饰符:
不能在顶级声明中设置。
在类中受保护的声明只能在其子类中访问。

open class Pr{protected val i = 0
}class Another : Pr{fun iValue() : Int{return i}
}

Classes which are not a subclass of Pr cannot access i

不是Pr的子类的类不能访问i

Declarations that are protected, when overridden would have the same protected modifier in the subclass unless you explicitly change them.

除非您明确更改,否则被覆盖的受保护声明在子类中将具有相同的protected修饰符。

open class Pr{open protected val i = 0
}class Another : Pr(){fun iValue() : Int{return i}override val i = 5 //protected visibility
}

The concept of Protected Modifiers in Kotlin that’s defined above differs from that in Java.

上面定义的Kotlin中的保护修饰符概念不同于Java中的概念。

内部修饰符 (Internal Modifier)

Internal is a new modifier available in Kotlin that’s not there in Java.
Setting a declaration as internal means that it’ll be available in the same module only.
By module in Kotlin, we mean a group of files that are compiled together.

Internal是Kotlin中可用的新修饰符,而Java中不存在。
将声明设置为内部意味着它将仅在同一模块中可用。
所谓Kotlin中的模块,是指一组一起编译的文件。

internal class A {
}internal val x = 0

These won’t be visible outside the current module.
Internal Modifiers is useful when you need to hide specific library implementations from the users.
This wasn’t possible using the package-private visibility in Java.

这些在当前模块之外不可见。
当您需要向用户隐藏特定的库实现时,“内部修饰符”很有用。
使用Java中的程序包专用可见性是不可能的。

私人修饰符 (Private Modifiers)

Private Modifiers do not allow the declarations to be visible outside the current scope.

私有修饰符不允许声明在当前作用域之外可见。

var setterVisibility: String = "abc"
private setopen class Pr{open protected val i = 0fun iValue() : Int{setterVisibility = 10return setterVisibility}
}

Since kotlin allows multiple top level definitions the above code works.

由于kotlin允许多个顶级定义,因此上述代码适用。

The below doesn’t

以下不

private open class ABC{private val x = 6
}private class BDE : ABC()
{fun getX(){return x //x cannot be accessed here.}
}

x is visibile only from inside its class.

x仅在其类内部可见。

We can set private constructors in the following way:

我们可以通过以下方式设置私有构造函数:

class PRIV private constructor(a: String) {...
}

By default classes have public constructors. Wherever the class goes the constructor follows.
We need to set the visibility modifier on the constructor in the definition itself.

默认情况下,类具有公共构造函数。 该类到哪里去,构造函数都将遵循。
我们需要在定义本身的构造函数中设置可见性修饰符。

Thus Kotlin uses the protected and internal modifiers differently from Java. Also Java’s default modifier is package-private which doesn’t exist in Kotlin, yet.

因此,Kotlin与Java不同地使用了protected和internal修饰符。 Java的默认修饰符是package-private,但Kotlin中尚不存在。

This brings an end to this quick tutorial on Visibility Modifiers in Kotlin.

这结束了有关Kotlin中的“可见性修改器”的快速教程。

翻译自: https://www.journaldev.com/20235/kotlin-visibility-modifiers-public-protected-internal-private

kotlin set 私有

kotlin set 私有_Kotlin可见性修改器–公共,受保护,内部,私有相关推荐

  1. gg 修改器游戏被保护_王者荣耀安卓和IOS修改超长游戏名字方法

    王者荣耀安卓和IOS修改超长游戏名字方法 21个字超长游戏昵称代码 方法在文末 者荣耀中,游 戏ID虽然并不是被玩家所看重,但是更多的00后玩家喜欢一些酷炫一些并且与众不同的名字,因此又不少玩家都尝试 ...

  2. gg 修改器游戏被保护_GG修改器sky光遇脚本下载app_GG修改器光遇脚本2020最新版下载 安卓版 V411.41.41...

    GG修改器sky光遇脚本是一款知名的手机游戏修改软件,这款手机软件十分强劲,拥有它你能随时一键游戏修改值,加快手机游戏这些作用,使你在这儿感受到一站式的游戏辅助软件感受.这个脚本app不需要root权 ...

  3. gg 修改器游戏被保护_修改器:怎样修改迷你世界皮肤?

    相信大家看到别人用修改器的时候,或多或少都希望自己也能用,可却不会,下面我主要教大家如何修改迷你世界皮肤. 1:首先你需要有一个修改器.拥有修改器以后,进入迷你世界,用哪一个号都行,来到主页面,进入& ...

  4. gg 修改器游戏被保护_和平精英120帧画质修改器下载

    和平精英120帧画质修改器是一款修改和平精英帧数为120帧的软件,和平精英120帧画质修改器让所有和平精英的版本修改成120帧,让游戏画面可以更加流畅,无论是国服.体验服.国际服.越南服.台服都支持, ...

  5. gg 修改器游戏被保护_火柴人战争遗产无敌版修改器下载-火柴人战争遗产修改器手机版下载...

    详情 火柴人战争遗产无敌版修改器是一款游戏辅助工具,在这款手游中修改就能请送货的无限钻石.金币等属性,简单的操作安全无污染,让你成为最强大的存在,内置修改器完全不会被封号哦,免费开箱子,强制升级.训练 ...

  6. gg 修改器游戏被保护_1.GG修改器的使用以及lua脚本编写

    让我们来认识一下GG脚本执行的功能 下面这一段就是我们常见的脚本执行的一个功能 请看注释讲解 ----------------- gg.clearResults() gg.setRanges(gg.R ...

  7. gg 修改器游戏被保护_某游戏DLL保护分析,以及偷学一点Unity代码保护思路

    本人虽然是个手残,却非常喜欢尝试各种音游,即使被虐到爆炸也停不下来. 最近看上一款某渊的音游,它的判定线移动打拍的玩法挺不错的,于是乎又手贱买了,然后被虐到体无完肤. 本着至少要给自己爽一下的原则,就 ...

  8. kotlin 类构造函数_Kotlin类– Kotlin构造函数

    kotlin 类构造函数 In this tutorial, we'll be discussing the Object Oriented Programming concepts of Kotli ...

  9. Windows 10 应用隐私声明——MVA字幕修改器

    此App全名为:MVA字幕修改器 MVA字幕修改器非常强调保护用户的隐私.当您在使用我们的服务时,我们将致力于为您提供最可靠的隐私保护措施.我们希望用户在使用各项服务之前能够仔细阅读本协议.用户一旦选 ...

最新文章

  1. 2013年的技术发展趋势
  2. react-native 查看对象属性
  3. 根据参数显示类别(三级联动,需要JSON数据)
  4. 训练生成对抗网络的过程中,训练gan的地方为什么这里没有detach,怎么保证训练生成器的时候不会改变判别器
  5. TCL withSNPS info existscreate_cellcreate_netconnect_net
  6. sql网站路径php,如何在源码中找出sql语句的位置呢
  7. php simpledateformat,Java中SimpleDateFormat的用法介绍(代码示例)
  8. java文件定时读写_java定时任务及日志的使用
  9. android 空白占位符,android textview空格占位符以及一些其他占位符汇总
  10. IPv6名称到地址的转换函数getaddrinfo()详解
  11. java threadlocal用法_Java ThreadLocal的用法解析
  12. 提高网页打开速度的一些小技巧
  13. 读李云同学的博文:无能的很可能正是我们自己
  14. RHEL 6和RHEL 7管理服务的区别
  15. pycharm收费版和 免费版的区别
  16. 论二级域名收集的各种姿势
  17. MFC(CFile)文件操作
  18. 了解爬虫的风险与以及如何规避风险-Java网络爬虫系统性学习与实战系列(3)
  19. SAP FICO 财务月结--自动清账
  20. Houdini 过程化地形系统(二):基于UE4的FC5植被系统(1)

热门文章

  1. 技术专题之-技术概述的目录
  2. 电影'社交网络'获金球奖最佳影片,最佳编剧,最佳导演,最佳配乐奖
  3. Windows API-GDI入门基础知识详解(1)
  4. 关于MATLAB实现的数字信号处理(四)
  5. [转载] python四种列表的插入方法及其效率
  6. [转载] 五、字符串类的实现及子串查找算法
  7. [转载] python 使用pandas进行读写excel文件操作
  8. Java调用Javascript、Python算法总结
  9. Hibernate中的3中状态
  10. 使用java实现面向对象 第七章