官术网_书友最值得收藏!

Access levels

All types and type members have accessibility levels, which constrain where they can be used. As mentioned earlier, not specifying the access level defaults to public. Kotlin comes with three different access levels, which are as follows:

  • Internal: This means you can create a new instance of your class from anywhere within your module
  • Private: This is more restrictive than the previous one because a class is only visible in the scope of the file defining it
  • Protected: You can use this accessibility level only for subclasses; it is not available for the file-level type of declaration

This differs from Java, which does not offer internal, and defaults to package-private.

The internal access level is the equivalent of private for classes when it comes to encapsulation; only this time it is at the module level. A class, interface, and so on can be made visible at the module level if the code isn't accessed from outside the module—for example, if the code is used purely for internal logic. This reduces the API you expose and makes it easier to understand. Furthermore, if a change is required in your module, you can assume that modifying the contract will only break the internal API of the assembly.

主站蜘蛛池模板: 牡丹江市| 石城县| 新建县| 盐山县| 滕州市| 永仁县| 来安县| 龙陵县| 耿马| 阜宁县| 白山市| 伊宁县| 宣化县| 辽阳市| 潜江市| 郁南县| 天全县| 永平县| 资中县| 略阳县| 高台县| 余庆县| 松潘县| 樟树市| 平遥县| 图木舒克市| 章丘市| 南通市| 洛扎县| 广河县| 平江县| 长海县| 个旧市| 安阳市| 河西区| 岑巩县| 图片| 昭平县| 涿鹿县| 徐汇区| 古蔺县|