1. 程式人生 > >scala進階25-獲取型別(類、物件、object、trait)

scala進階25-獲取型別(類、物件、object、trait)

import scala.reflect.runtime.universe._

class Spark
trait Hadoop
object Flink
class Java {
  class Scala
}

/**
  * 例項獲取類用getClass
  * 類獲取用classOf
  */
object Type_Advanced {
  def main(args: Array[String]): Unit = {
//    println(typeOf[Spark]) //報錯
    println(classOf[Spark])

    val spark = new Spark
    println(spark.getClass)

    println(classOf[Hadoop])
    println(Flink.getClass) //class Flink$ 說明object是有對應的類的
//    println(classOf[Flink]) //報錯,找不到類,

    val java1 = new Java
    val java2 = new Java
    val scala1 = new java1.Scala
    val scala2 = new java2.Scala

    println(java1.getClass)
    println(java2.getClass)
//    println(typeOf[java1.Scala]) //報錯,找不到類
//    println(typeOf[java2.Scala])

    println(classOf[List[Int]])
  }
}