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

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.

主站蜘蛛池模板: 珠海市| 宝山区| 五莲县| 瑞昌市| 巴东县| 鹤壁市| 长春市| 浦东新区| 海晏县| 凯里市| 砀山县| 分宜县| 芜湖县| 板桥市| 德州市| 墨竹工卡县| 定安县| 黑山县| 嘉义市| 类乌齐县| 巫山县| 嵩明县| 哈巴河县| 茌平县| 昌邑市| 昭通市| 六枝特区| 东乌珠穆沁旗| 罗江县| 北辰区| 绍兴市| 济宁市| 宁陵县| 中西区| 汶川县| 曲水县| 明水县| 庐江县| 宽城| 工布江达县| 文昌市|