- Learn Kotlin Programming(Second Edition)
- Stephen Samuel Stefan Bocutiu
- 236字
- 2021-06-24 14:13:27
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.
- PHP程序設計(慕課版)
- Java從入門到精通(第4版)
- Dependency Injection in .NET Core 2.0
- YARN Essentials
- Mastering Python High Performance
- SharePoint Development with the SharePoint Framework
- 蘋果的產品設計之道:創建優秀產品、服務和用戶體驗的七個原則
- Python爬蟲、數據分析與可視化:工具詳解與案例實戰
- Frank Kane's Taming Big Data with Apache Spark and Python
- Android應用開發實戰
- Python無監督學習
- 一步一步學Spring Boot:微服務項目實戰(第2版)
- SOA Patterns with BizTalk Server 2013 and Microsoft Azure(Second Edition)
- 區塊鏈原理與技術應用
- Java EE 7 First Look