代码之家  ›  专栏  ›  技术社区  ›  Michael Rutherfurd

“静态this”关键字会带来哪些问题或好处?

  •  0
  • Michael Rutherfurd  · 技术社区  · 14 年前

    class Example {
        public static void main(String[] args) {
            assert Example.class == thisclass.class
        }
    }
    

    其中“this class”是返回当前类的关键字,就像“this”返回当前实例一样。

    我认为有充分的理由不在Java中包含这个概念,但是它们是什么呢?另外,使用Groovy元编程是否可以做到这一点?

    2 回复  |  直到 11 年前
        1
  •  2
  •   hvgotcodes    14 年前

    groovy具有您刚才所说的“this”语义。在静态上下文中,“this”指的是方法所在的类。

    退房 http://groovy.codehaus.org/Differences+from+Java 并搜索“this keyword”。

    显然,Java并没有遵循这一点,可能是因为它最终只是一个很小的特色,那就是“groovy”。当然没有任何必要。

        2
  •  0
  •   user207421    14 年前