包的应用及import

62 阅读1分钟
// 1. 导入一个类
// import tools.A

// 2. 导入多个
// import 包名.{类1, 类2 ……}
// import tools.{B, C, Student}

// 3. 导入一个包下所有的类 包名._
import tools._

// 4. 导入之后重新命名 包名.{类名=>新名字}
import tools.Student => NewStudent

object bao1 {
  def main(args: Array[String]): Unit = {
    new A()
    new B()
    new C()

    var s1 = new NewStudent()
    s1.test()
  }
}
  • 当你把文件移动到新的包目录下,工具会自动识别新包的路径,直接更新文件开头的package语句。
  • 若手动修改了包目录结构,只需刷新项目(比如 IDEA 里右键项目选 “Reload Project”),工具也会同步更新package说明。