首先写一个样例类Demo
package com.ruozedata.bigdata.scala04.casepkg
object CaseClassDemo01 {
def main(args: Array[String]): Unit = {
print("CaseClass")
}
}
abstract class Amount
case class Dollar(value: String) extends Amount
case class Currency(value: Double, unit: String) extends Amount
case object NoAmount extends Amount
运行后,使用反编译打开,会发现一个样例类 Dollar会生成Dollar$.class和Dollar.class文件
首先来看Dollar$.class
package com.ruozedata.bigdata.scala04.casepkg;
import scala.Option;
import scala.Serializable;
import scala.Some;
import scala.runtime.AbstractFunction1;
public final class Dollar$ extends AbstractFunction1 implements Serializable {
public static final Dollar$ MODULE$;
public final String toString() {
return "Dollar";
}
public Dollar apply(String value) {
return new Dollar(value);
}
public Option unapply(Dollar x$0) {
return (x$0 == null) ? (Option)scala.None$.MODULE$ : (Option)new Some(x$0.value());
}
private Object readResolve() {
return MODULE$;
}
private Dollar$() {
MODULE$ = this;
}
}
然后Dollar.class
package com.ruozedata.bigdata.scala04.casepkg;
import scala.Function1;
import scala.Option;
import scala.Product;
import scala.Serializable;
import scala.collection.Iterator;
import scala.reflect.ScalaSignature;
import scala.runtime.BoxesRunTime;
import scala.runtime.ScalaRunTime$;
@ScalaSignature(bytes = "\006\001\005}a\001B\001\003\0016\021a\001R8mY\006\024(BA\002\005\003\035\031\027m]3qW\036T!!\002\004\002\017M\034\027\r\\11i)\021q\001C\001\bE&TX\rZ1uC*\t1\"A\002d_6\034\001a\005\003\001\035IA\002CA\b\021\033\005\021\021BA\t\003\005\031\tUn\\;oiB\0211CF\007\002))\tQ#A\003tG\006d\027-\003\002\030)\t9\001K]8ek\016$\bCA\n\032\023\tQBC\001\007TKJL\027\r\\5{C\ndW\r\003\005\035\001\tU\r\021\"\001\036\003\0251\030\r\\;f+\005q\002CA\020#\035\t\031\002%\003\002\")\0051\001K]3eK\032L!a\t\023\003\rM#(/\0338h\025\t\tC\003\003\005'\001\tE\t\025!\003\037\003\0311\030\r\\;fA!)\001\006\001C\001S\0051A(\0338jiz\"\"AK\026\021\005=\001\001\"\002\017(\001\004q\002bB\027\001\003\003%\tAL\001\005G>\004\030\020\006\002+_!9A\004\fI\001\002\004q\002bB\031\001#\003%\tAM\001\017G>\004\030\020\n3fM\006,H\016\036\0232+\005\031$F\001\0205W\005)\004C\001\034<\033\0059$B\001\035:\003%)hn\0315fG.,GM\003\002;)\005Q\021M\0348pi\006$\030n\0348\n\005q:$!E;oG\",7m[3e-\006\024\030.\0318dK\"9a\bAA\001\n\003z\024!\0049s_\022,8\r\036)sK\032L\0070F\001A!\t\te)D\001C\025\t\031E)\001\003mC:<'\"A#\002\t)\fg/Y\005\003G\tCq\001\023\001\002\002\023\005\021*\001\007qe>$Wo\031;Be&$\0300F\001K!\t\0312*\003\002M)\t\031\021J\034;\t\0179\003\021\021!C\001\037\006q\001O]8ek\016$X\t\\3nK:$HC\001)T!\t\031\022+\003\002S)\t\031\021I\\=\t\017Qk\025\021!a\001\025\006\031\001\020J\031\t\017Y\003\021\021!C!/\006y\001O]8ek\016$\030\n^3sCR|'/F\001Y!\rIF\fU\007\0025*\0211\fF\001\013G>dG.Z2uS>t\027BA/[\005!IE/\032:bi>\024\bbB0\001\003\003%\t\001Y\001\tG\006tW)];bYR\021\021\r\032\t\003'\tL!a\031\013\003\017\t{w\016\\3b]\"9AKXA\001\002\004\001\006b\0024\001\003\003%\teZ\001\tQ\006\034\bnQ8eKR\t!\nC\004j\001\005\005I\021\t6\002\021Q|7\013\036:j]\036$\022\001\021\005\bY\002\t\t\021\"\021n\003\031)\027/^1mgR\021\021M\034\005\b).\f\t\0211\001Q\017\035\001(!!A\t\002E\fa\001R8mY\006\024\bCA\bs\r\035\t!!!A\t\002M\0342A\035;\031!\021)\bP\b\026\016\003YT!a\036\013\002\017I,h\016^5nK&\021\021P\036\002\022\003\n\034HO]1di\032+hn\031;j_:\f\004\"\002\025s\t\003YH#A9\t\017%\024\030\021!C#U\"9aP]A\001\n\003{\030!B1qa2LHc\001\026\002\002!)A$ a\001=!I\021Q\001:\002\002\023\005\025qA\001\bk:\f\007\017\0357z)\021\tI!a\004\021\tM\tYAH\005\004\003\033!\"AB(qi&|g\016C\005\002\022\005\r\021\021!a\001U\005\031\001\020\n\031\t\023\005U!/!A\005\n\005]\021a\003:fC\022\024Vm]8mm\026$\"!!\007\021\007\005\013Y\"C\002\002\036\t\023aa\0242kK\016$\b")
public class Dollar extends Amount implements Product, Serializable {
private final String value;
public static Option unapply(Dollar paramDollar) {
return Dollar$.MODULE$.unapply(paramDollar);
}
public static Dollar apply(String paramString) {
return Dollar$.MODULE$.apply(paramString);
}
public static Function1 andThen(Function1 paramFunction1) {
return Dollar$.MODULE$.andThen(paramFunction1);
}
public static Function1 compose(Function1 paramFunction1) {
return Dollar$.MODULE$.compose(paramFunction1);
}
public String value() {
return this.value;
}
public Dollar copy(String value) {
return new Dollar(value);
}
public String copy$default$1() {
return value();
}
public String productPrefix() {
return "Dollar";
}
public int productArity() {
return 1;
}
public Object productElement(int x$1) {
int i = x$1;
switch (i) {
default:
throw new IndexOutOfBoundsException(BoxesRunTime.boxToInteger(x$1).toString());
case 0:
break;
}
return value();
}
public Iterator
1、样例类默认实现了序列化,实现了scala的Product
2、构造器中的value当成了属性,且默认都是val(对应Java的final)
3、默认实现了apply和unapply,这就是为何样例类不用new可以直接使用的原
4、默认实现了常用的toString/equals/hashCode/copy方法