Scala case class 字段

Scala case class fields

我正在尝试了解 Scala 案例 class 与常规 class 的区别。

例如我有一个定义

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

并且可以像 carge.cccharge.amount 一样使用它。

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

案例 class 是 "product type"。把它想象成元组,元素被命名,而不是被索引。从技术上讲,是的,cc 是一个生成的访问器函数,但不,你不能重新定义它。如果可以的话,它会破坏它作为一个案例的目的 class 开始。

只是做这样的事情:

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

使用代码内容创建文件 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(): Int = Charge.this.cc();
    <synthetic> def copy$default(): Double = Charge.this.amount();
    override <synthetic> def productPrefix(): String = "Charge";
    <synthetic> def productArity(): Int = 2;
    <synthetic> def productElement(x: Int): Object = {
      case <synthetic> val x1: Int = x;
      (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).toString())
      }
    };
    override <synthetic> def productIterator(): Iterator = runtime.this.ScalaRunTime.typedProductIterator(Charge.this);
    <synthetic> def canEqual(x: Object): Boolean = x.$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: Object): Boolean = Charge.this.eq(x).||({
  case <synthetic> val x1: Object = x;
  case5(){
    if (x1.$isInstanceOf[Charge]())
      matchEnd4(true)
    else
      case6()
  };
  case6(){
    matchEnd4(false)
  };
  matchEnd4(x: Boolean){
    x
  }
}.&&({
      <synthetic> val Charge: Charge = x.$asInstanceOf[Charge]();
      Charge.this.cc().==(Charge.cc()).&&(Charge.this.amount().==(Charge.amount())).&&(Charge.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[=10=]: Charge): Option = if (x[=10=].==(null))
      scala.this.None
    else
      new Some(new Tuple2$mcID$sp(x[=10=].cc(), x[=10=].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>();
      ()
    }
  }
}

它告诉您 this.cc 是通过一个名为 cc().

的方法从对象的实际 属性 提供的