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

Wildcard imports

If we have a bunch of imports from the same package, then to avoid specifying each import individually, we can import the entire package at once using the * operator.

If we have several imports from the same package, then to avoid listing each import on a single line, all the classes and interfaces from a package can be imported at once. This is done using the * operator. For example, the following package declaration would import every class and interface from the com.packt.myproject package:

    import com.packt.myproject.* 

Wildcard imports are especially useful when a large number of helper functions or constants are defined at the top level, and we wish to refer to those without using the name of the package that contains those functions. In the following example, two constants will be defined in one package:

    package com.packt.myproject.constants 
    // define the two constants
    val PI = 3.142 
    val E = 2.178 

To avoid referencing these constants by their FQN, we will import them in the next example:

 package com.packt.myproject
//import the two constants defined import com.packt.myproject.constants.* //sum the two constants fun add() = E + PI

Notice how the add() function does not need to refer to E and PI using the FQN, but can simply use them as if they were in scope. The wildcard import removes the repetition that would, otherwise, be needed when importing numerous constants.

主站蜘蛛池模板: 扎囊县| 博湖县| 大同县| 营口市| 巴楚县| 吉安市| 中卫市| 诸城市| 林芝县| 临海市| 台湾省| 永嘉县| 元阳县| 石城县| 灵山县| 广宗县| 信阳市| 长阳| 枣阳市| 旺苍县| 喜德县| 楚雄市| 普格县| 文山县| 沙坪坝区| 正蓝旗| 遂平县| 成武县| 灵宝市| 马鞍山市| 桂平市| 庐江县| 延川县| 堆龙德庆县| 荥经县| 淮阳县| 新巴尔虎左旗| 上饶市| 左权县| 独山县| 连城县|