2017-08-01 105 views
-1

我想了解一个普通类的斯卡拉案例类的差异。斯卡拉案例类字段

E.g.我有一个定义

case class Charge(cc: CreditCard, amount: Double)

,并可以使用它像carge.cccharge.amount

这些语句是常量字段引用还是实际隐藏的getter被使用?是否有可能重新定义例如carge.cc在返回值之前添加一些代码?

+0

关于案例类有很多很好的在线文档,就像[“official”之一](http://docs.scala-lang.org/tutorials/tour/case-classes.html)以 – cchantep

+0

开头@cchantep我已经阅读过,无法得到我的问题的答案。 –

回答

1

案例类别是“产品类型”。可以把它看作元组,用元素命名,而不是索引。从技术上讲,是的,cc是一个生成的存取函数,但是不能重新定义它。如果可以的话,它将打败作为案例课堂的目的。

就做这样的事情,而不是:

class Charge(_cc: CreditCard, _amount: Double) { 
    def cc = modify(_cc) 
    def amount = addTips(_amount) 
} 
0

与您的代码 的内容创建文件Charge.scala,然后使用scalac -print Charge.scala编译代码,你会得到:

[syntax trees at end of     cleanup]] // Test.scala 
package <empty> { 
    case class Charge extends Object with Product with Serializable { 
    <caseaccessor> <paramaccessor> private[this] val cc: Int = _; 
    <stable> <caseaccessor> <accessor> <paramaccessor> def cc(): Int = Charge.this.cc; 
    <caseaccessor> <paramaccessor> private[this] val amount: Double = _; 
    <stable> <caseaccessor> <accessor> <paramaccessor> def amount(): Double = Charge.this.amount; 
    <synthetic> def copy(cc: Int, amount: Double): Charge = new Charge(cc, amount); 
    <synthetic> def copy$default$1(): Int = Charge.this.cc(); 
    <synthetic> def copy$default$2(): Double = Charge.this.amount(); 
    override <synthetic> def productPrefix(): String = "Charge"; 
    <synthetic> def productArity(): Int = 2; 
    <synthetic> def productElement(x$1: Int): Object = { 
     case <synthetic> val x1: Int = x$1; 
     (x1: Int) match { 
     case 0 => scala.Int.box(Charge.this.cc()) 
     case 1 => scala.Double.box(Charge.this.amount()) 
     case _ => throw new IndexOutOfBoundsException(scala.Int.box(x$1).toString()) 
     } 
    }; 
    override <synthetic> def productIterator(): Iterator = runtime.this.ScalaRunTime.typedProductIterator(Charge.this); 
    <synthetic> def canEqual(x$1: Object): Boolean = x$1.$isInstanceOf[Charge](); 
    override <synthetic> def hashCode(): Int = { 
     <synthetic> var acc: Int = -889275714; 
     acc = Statics.this.mix(acc, Charge.this.cc()); 
     acc = Statics.this.mix(acc, Statics.this.doubleHash(Charge.this.amount())); 
     Statics.this.finalizeHash(acc, 2) 
    }; 
    override <synthetic> def toString(): String = ScalaRunTime.this._toString(Charge.this); 
    override <synthetic> def equals(x$1: Object): Boolean = Charge.this.eq(x$1).||({ 
    case <synthetic> val x1: Object = x$1; 
    case5(){ 
    if (x1.$isInstanceOf[Charge]()) 
     matchEnd4(true) 
    else 
     case6() 
    }; 
    case6(){ 
    matchEnd4(false) 
    }; 
    matchEnd4(x: Boolean){ 
    x 
    } 
}.&&({ 
     <synthetic> val Charge$1: Charge = x$1.$asInstanceOf[Charge](); 
     Charge.this.cc().==(Charge$1.cc()).&&(Charge.this.amount().==(Charge$1.amount())).&&(Charge$1.canEqual(Charge.this)) 
    })); 
    def <init>(cc: Int, amount: Double): Charge = { 
     Charge.this.cc = cc; 
     Charge.this.amount = amount; 
     Charge.super.<init>(); 
     scala.Product$class./*Product$class*/$init$(Charge.this); 
    () 
    } 
    }; 
    <synthetic> object Charge extends scala.runtime.AbstractFunction2 with Serializable { 
    final override <synthetic> def toString(): String = "Charge"; 
    case <synthetic> def apply(cc: Int, amount: Double): Charge = new Charge(cc, amount); 
    case <synthetic> def unapply(x$0: Charge): Option = if (x$0.==(null)) 
     scala.this.None 
    else 
     new Some(new Tuple2$mcID$sp(x$0.cc(), x$0.amount())); 
    <synthetic> private def readResolve(): Object = Charge; 
    case <synthetic> <bridge> <artifact> def apply(v1: Object, v2: Object): Object = Charge.this.apply(scala.Int.unbox(v1), scala.Double.unbox(v2)); 
    def <init>(): Charge.type = { 
     Charge.super.<init>(); 
    () 
    } 
    } 
} 

告诉你通过cc()的方法从对象的实际属性中提供了this.cc