scala进阶12-复合类型

本文深入探讨Scala中的复合类型概念,包括如何定义复合类型、使用复合类型进行方法参数传递及返回值处理等。通过实例展示了复合类型的灵活性及其在实际编程中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 }
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值