伴生对象

start

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 编写Hello.scala
/** 1. object表示一个伴生对象,可以理解就是一个对象
* 2. Hello就是对象名字,它底层真正对应的类名是Hello$,对象Hello$类型的一个静态对象MODULE$
* 3. 当我们编写一个object底层会生成
* */
object Hello {
def main(args: Array[String]): Unit = {
print("hello world")
}
}

# 编译Hello.scala
scalac Hello.scala
# 生成如下
Hello$.class Hello.class Hello.scala
# 安装jd-gui
brew cask install jd-gui
# 反编译Hello$.class
public final class Hello$ { public static final Hello$ MODULE$ = new Hello$();
public void main(String[] args) { // Byte code:
// 0: getstatic scala/Predef$.MODULE$ : Lscala/Predef$;
// 3: ldc 'hello world'
// 5: invokevirtual print : (Ljava/lang/Object;)V
// 8: return
// Line number table:
// Java source line number -> byte code offset
// #3 -> 0
// Local variable table:
// start length slot name descriptor
// 0 9 0 this LHello$;
// 0 9 1 args [Ljava/lang/String;
}
}

# 反编译Hello.class
import scala.reflect.ScalaSignature;

@ScalaSignature(bytes = "\006\005%:Q\001B\003\t\002!1QAC\003\t\002-AQAE\001\005\002MAQ\001F\001\005\002U\tQ\001S3mY>T\021AB\001\by\025l\007\017^=?\007\001\001\"!C\001\016\003\025\021Q\001S3mY>\034\"!\001\007\021\0055\001R\"\001\b\013\003=\tQa]2bY\006L!!\005\b\003\r\005s\027PU3g\003\031a\024N\\5u}Q\t\001\"\001\003nC&tGC\001\f\032!\tiq#\003\002\031\035\t!QK\\5u\021\025Q2\0011\001\034\003\021\t'oZ:\021\0075ab$\003\002\036\035\t)\021I\035:bsB\021qD\n\b\003A\021\002\"!\t\b\016\003\tR!aI\004\002\rq\022xn\034;?\023\t)c\"\001\004Qe\026$WMZ\005\003O!\022aa\025;sS:<'BA\023\017\001")
public final class Hello {
public static void main(String[] paramArrayOfString) {
Hello$.MODULE$.main(paramArrayOfString);
}
}