case class 和 class的区别

//*case class和class的区别
case class Calculator(brand:String,val model:String){}//无需声明val也可以声明成员常量
// 1.caseclass内置了apply函数,构造时无需加new,而class需要定义其伴生对象,通过object Calculator 中的apply函数才可以直接通过类名加括号和参数的形式构造新实例

val hp20b = new Calculator("HP","20B")
val hp30b = Calculator("HP","30B")//这里其实无需加new了,caseclass 内置apply函数

def calcType(calc: Calculator) = calcmatch {
  case Calculator("HP","20B") => "financial"
 
case
Calculator("HP","48G") => "scientific"
 
case
Calculator("HP","30B") => "business"
 
case
Calculator(ourBrand, ourModel) =>"Calculator: %s %s is of unknowntype".format(ourBrand,ourModel)
  case Calculator(_, _) =>"Calculator of unknown type"//匹配其他未列举的Calculator
 
case _=> "Calculator ofunknown type"//可以匹配到Calculator的子类
 
case c@Calculator(_,_) =>"Calculator: %sof unknown type".format(c)//可以重新绑定calc的名字
}
calcType(hp20b)
calcType(hp30b)

// 2. 而且构造器的变量名无需声明val即为成员常量,可以在类定义外访问hp30b.brand
hp30b.brand//brand无需声明val


// 3.声明为caseclass或者case object才可以进行模式匹配,class不可以,不仅可以是通过类名匹配,还可以同一类名但通过不同的参数进行识别匹配,还可以是
//传入Any类的对象后进行匹配
def bigger(o: Any): Any = {
  o match {
    case i: Intifi < 0 => i - 1//i代表临时对象名,i:Int指明对象类型
   
case i: Int => i + 1
   
case d:Double if d < 0.0 =>d - 0.1//这一句比下一句先执行,所以下一句无需判断大小0
   
case d: Double => d +0.1
   
case text: String => text + "s"
 
}
}
class Calculator2(valbrand:String,val model:String){}

//通过类的成员来判断
def calcType2(calc: Calculator) = calcmatch {
  case _ ifcalc.brand == "HP" && calc.model =="20B" => "financial"
 
case
_ ifcalc.brand == "HP" && calc.model =="48G" => "scientific"
 
case
_ ifcalc.brand == "HP" && calc.model =="30B" => "business"
 
case
_=> "unknown"
}

 

实例Tree

 

//**case object和简单操作
sealed abstract class Tree
case class Node(left:Tree, right:Tree) extends Tree
case class Leaf[A](value:A) extends Tree
case object EmptyLeaf extends Tree

//方便的构建新的树对象
val treeA = Node(EmptyLeaf, Leaf(5))//Leaf自动识别传入对象为Int类型
val treeB = Node(Node(Leaf(2), Leaf(3)), Leaf(5))

//通过克隆修改树结构,scala的思想是immutable datatype 所以改变结构是复制旧树的部分或全部结构来创建新的tree,
val treeC = treeA.copy(left = treeB.left)//val treeA和treeB都是常量不会被改变

//美化输出
println("TreeA:" + treeA)
println("TreeB" + treeB)
println("TreeC" + treeC)

//对比
println("TreeA == TreeB: %s".format(treeA == treeB).toString)

//模式匹配
treeA match{
  case Node(EmptyLeaf, right) => println("can be reduced to" + right)
  case Node(left, EmptyLeaf) => println("can be reduced to " + left)
  case _ => println(treeA + "cannot be reduced")

}

//因为采用了sealed关键字,强制了编译器帮我们检查模式匹配中的所有的case情况
def checkTree(t:Tree) = t match{
  case Node(EmptyLeaf, Node(left, right)) =>
  // case Node(EmptyLeaf, Leaf(el)) =>
  case Node(Node(left, right), EmptyLeaf) =>
  case Node(Leaf(el), EmptyLeaf) =>
  case Node(Node(l1, r1), Node(l2, r2)) =>
  case Node(Leaf(e1), Leaf(e2)) =>
  case Node(Node(left, right), Leaf(el)) =>
  case Node(Leaf(el), Node(left, right)) =>
  // case Node(EmptyLeaf, EmptyLeaf) =>
  case Leaf(el) =>
//  case EmptyLeaf =>
}//注释了的三种情况都会被编译器检查出来并警告
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值