Why does groovy .class return a different value than .getClass()

后端 未结 2 1667
悲&欢浪女
悲&欢浪女 2020-12-17 14:31

According to http://groovy.codehaus.org/Things+you+can+do+but+better+leave+undone

  1. Accessing an object\'s type like a property
2条回答
  •  臣服心动
    2020-12-17 15:11

    A non-map example is the difference between the class of a type, and the class of an instance. The .class and .getClass() of an instance is its type, with some exceptions, e.g. maps. The .class of a type, is the type. The .getClass() of a type is java.lang.Class

    For example:

    def a = Integer.getClass()
    def b = Integer.class
    def c = 1.getClass()
    def d = 1.class    
    
    println a
    println b
    println c
    println d
    

    will give the output:

    class java.lang.Class
    class java.lang.Integer
    class java.lang.Integer
    class java.lang.Integer
    

提交回复
热议问题