包及其导入(2)

31 阅读1分钟

(一) 导入多个包

如果要导入多个类该怎么办:

// 1.import 导入一个类   包名.类名

// 2.import 导入多个类   包名.{类名1,类名2}

// 3.import 导入包下边的所有的类  包名._

代码演示:

package toolstest

import tools.{A,B,C}

object Main {
  def main(arg: Array[String]): Unit = {
    new A()
    new B()
    new C()
  }
}

(二) 类命名的冲突

如果导入的类与现有的类名有冲突怎么办:

方法:import 包名.{原包名 => 新包名}

代码演示(将包tools中重名的类C改成类C1):

import tools.{C => C1}