首先写一个样例类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<String, Dollar> implements Serializable {
public static final Dollar$ MODULE$;
public final String toString() {
return "Dollar";
}
public Dollar apply(String value) {
return new Dollar(value);
}
public Option<String> unapply(Dollar x$0) {
return (x$0 == null) ? (Option<String>)scala.None$.MODULE$ : (Option<String>)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&<G-\031;b\025\tI!\"A\005sk>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<String> unapply(Dollar paramDollar) {
return Dollar$.MODULE$.unapply(paramDollar);
}
public static Dollar apply(String paramString) {
return Dollar$.MODULE$.apply(paramString);
}
public static <A> Function1<String, A> andThen(Function1<Dollar, A> paramFunction1) {
return Dollar$.MODULE$.andThen(paramFunction1);
}
public static <A> Function1<A, Dollar> compose(Function1<A, String> 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<Object> productIterator() {
return ScalaRunTime$.MODULE$.typedProductIterator(this);
}
public boolean canEqual(Object x$1) {
return x$1 instanceof Dollar;
}
public int hashCode() {
return ScalaRunTime$.MODULE$._hashCode(this);
}
public String toString() {
return ScalaRunTime$.MODULE$._toString(this);
}
public boolean equals(Object x$1) {
// Byte code:
// 0: aload_0
// 1: aload_1
// 2: if_acmpeq -> 80
// 5: aload_1
// 6: astore_2
// 7: aload_2
// 8: instanceof com/ruozedata/bigdata/scala04/casepkg/Dollar
// 11: ifeq -> 19
// 14: iconst_1
// 15: istore_3
// 16: goto -> 21
// 19: iconst_0
// 20: istore_3
// 21: iload_3
// 22: ifeq -> 84
// 25: aload_1
// 26: checkcast com/ruozedata/bigdata/scala04/casepkg/Dollar
// 29: astore #4
// 31: aload_0
// 32: invokevirtual value : ()Ljava/lang/String;
// 35: aload #4
// 37: invokevirtual value : ()Ljava/lang/String;
// 40: astore #5
// 42: dup
// 43: ifnonnull -> 55
// 46: pop
// 47: aload #5
// 49: ifnull -> 63
// 52: goto -> 76
// 55: aload #5
// 57: invokevirtual equals : (Ljava/lang/Object;)Z
// 60: ifeq -> 76
// 63: aload #4
// 65: aload_0
// 66: invokevirtual canEqual : (Ljava/lang/Object;)Z
// 69: ifeq -> 76
// 72: iconst_1
// 73: goto -> 77
// 76: iconst_0
// 77: ifeq -> 84
// 80: iconst_1
// 81: goto -> 85
// 84: iconst_0
// 85: ireturn
// Line number table:
// Java source line number -> byte code offset
// #12 -> 0
// Local variable table:
// start length slot name descriptor
// 0 86 0 this Lcom/ruozedata/bigdata/scala04/casepkg/Dollar;
// 0 86 1 x$1 Ljava/lang/Object;
}
public Dollar(String value) {
Product.class.$init$(this);
}
}
1、样例类默认实现了序列化,实现了scala的Product
2、构造器中的value当成了属性,且默认都是val(对应Java的final)
3、默认实现了apply和unapply,这就是为何样例类不用new可以直接使用的原
4、默认实现了常用的toString/equals/hashCode/copy方法