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

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.

主站蜘蛛池模板: 廊坊市| 克什克腾旗| 五峰| 九江市| 仁布县| 常德市| 乌海市| 绍兴市| 盱眙县| 洞头县| 康保县| 商河县| 徐水县| 乐业县| 井研县| 承德市| 凤冈县| 桐柏县| 兰坪| 会泽县| 冕宁县| 托克托县| 山阴县| 盈江县| 东山县| 班玛县| 陵川县| 义马市| 富宁县| 吉林省| 临沧市| 尼玛县| 和政县| 鄂托克前旗| 新龙县| 金华市| 南华县| 墨竹工卡县| 新巴尔虎右旗| 宁津县| 安阳县|