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

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.

主站蜘蛛池模板: 雷州市| 白银市| 常山县| 宁明县| 龙陵县| 盐边县| 九江市| 霸州市| 勐海县| 二连浩特市| 游戏| 泗水县| 陕西省| 长兴县| 荥阳市| 岚皋县| 承德县| 日照市| 新密市| 漳平市| 崇州市| 宜兰县| 扎赉特旗| 台江县| 永靖县| 根河市| 玉环县| 丰县| 和林格尔县| 连平县| 错那县| 长寿区| 东源县| 杭州市| 牙克石市| 崇仁县| 黄浦区| 涿州市| 富川| 隆子县| 临安市|