Scala案例类字段

时间:2017-08-01 15:24:07

标签: scala case-class

我试图了解Scala案例类与普通类的区别。

E.g。我有一个定义

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

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

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

2 个答案:

答案 0 :(得分:1)

案例类是"产品类型"。把它想象成元组,用元素命名,而不是索引。从技术上讲,是的,cc是一个生成的访问函数,但不能重新定义它。如果可以的话,它将打破它作为案例类开始的目的。

只需做这样的事情:

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

答案 1 :(得分: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>();
      ()
    }
  }
}

告诉您this.cc是通过名为cc()的方法从对象的实际属性提供的。

相关问题