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]])
  }
}

你可能感兴趣的:(Scala)