“def Type”在常规类中的声明 [英] "def Type" declarations in groovy classes

查看:143
本文介绍了“def Type”在常规类中的声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

突然间,我意识到我可以写出

  class Person {
def字符串名称
}

我的问题是 - 上面提供的代码与经典代码有什么不同?

  class Person {
字符串名称
}

为什么第一个表单甚至存在?

解决方案

没有区别。将 def 添加到类型定义的开头什么都不做。然而,这是解析器允许的。



检查这种东西的一种方法是启动groovyConsole并运行AST浏览器(对于脚本)

  class Person {
def字符串名称
}



显示:

  public class Person extends java。 lang.Object {

private java.lang.String name

}


Suddenly I've realized that I can write

class Person {
     def String name
}

My question is - what is the difference between code provided above and classic:

class Person {
   String name
}

Why first form even exists?

解决方案

There's no difference. Adding def to the beginning of a type definition does nothing. However, it is allowed by the parser.

One way to check stuff like this, is to fire up the groovyConsole, and run the AST Browser (which for the script)

class Person {
     def String name
}

Shows:

public class Person extends java.lang.Object { 

    private java.lang.String name 

}

这篇关于“def Type”在常规类中的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆