跟java中一样,scala中也有包的概念,主要作用也差不多,但是还是有所区别的:
scala中支持package的嵌套
package spark.navigation {
abstract class Navigator{ //package路径:spark.navigation.Navigator
def act
}
package tests {
class NavigatorSuite //package路径:spark.navigation.test.NavigatorSuite
}
package impls {
class Action extends Navigator{ //package路径:spark.navigation.impl.NavigatorSuite
def act = println("Action")
}
}
}
包对象:
package com.scala.spark
package object people {
val defaultName = "Scala"
}
package people {
class people {
var name = defaultName //people包下的people class能访问pepole包对象里面定义的所有内容(属性、方法)
}
}
包的隐式引用:
import java.lang._ //引用lang包下的所有东西
import scala._ //scala包下的所有东西
import Predef._ //Predef对象的所有东西
其它:
import java.awt.{Color,Font} //引用包下特定类
import java.util.{HashMap=>JavaHashMap} //定义别名
import scala.{StringBuilder => _} //屏蔽scala包下的StringBuilder
网友评论