trait Compound_Type1
trait Compound_Type2
class Compound_Type extends Compound_Type1 with Compound_Type2
object Compound_Type {
//参数x是复合类型,即是type1的类型也是type2的类型
def compound_Type(x: Compound_Type1 with Compound_Type2) = {
println("compound type in globle method")
}
def main(args: Array[String]): Unit = {
//函数调用时也必须是复合类型
compound_Type(new Compound_Type1 with Compound_Type2)
//传入非匿名对象
object compound_Type_object extends Compound_Type1 with Compound_Type2
compound_Type(compound_Type_object)
//用type定义别名(引用),使代码更优雅
type compound_Type_Alias = Compound_Type1 with Compound_Type2
def compound_Type_Local(x: compound_Type_Alias) = println("compound type in local method")
val compound_Type_Class = new Compound_Type
compound_Type_Local(compound_Type_Class)
//复合类型和结构类型共用
type Scala = Compound_Type1 with Compound_Type2 { def init(): Unit }
}
}
scala进阶12-复合类型
最新推荐文章于 2022-05-30 09:48:42 发布