How the get the classOf for a scala object type

前端 未结 3 1622
花落未央
花落未央 2020-12-28 11:25

I wonder how to get a class object for an object type in Scala. Ok, that is a mouth full because of the double meaning for object. So here an example which will fail:

<
相关标签:
3条回答
  • 2020-12-28 12:05

    The reason why classOf[Main] doesn't work is because Main is not a type.

    Classes and traits define types, objects do not.

    0 讨论(0)
  • 2020-12-28 12:11

    Since Main is an object, for your example to work, simply replace your assignment line with;

    private [this] val TAG = this.getClass.getName;
    
    0 讨论(0)
  • 2020-12-28 12:13
    scala> Main.getClass
    res1: java.lang.Class[_] = class Main$
    
    0 讨论(0)
提交回复
热议问题