加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

What is this "def" I heard of?

发布时间:2020-12-14 16:59:10 所属栏目:大数据 来源:网络整理
导读:What is this "def" I heard of? "def" is a replacement for a type name. In variable definitions it is used to indicate that you don't care about the type. In variable definitions it is mandatory to either provide a type name explicitly or t

What is this "def" I heard of?
"def" is a replacement for a type name. In variable definitions it is used to indicate that you don't care
about the type. In variable definitions it is mandatory to either provide a type name explicitly or to use
"def" in replacement. This is needed to the make variable definitions detectable for the Groovy parser.
These definitions may occur for local variables in a script or for local variables and properties/fields in a
class.
Rule of thumb
You can think of "def" as an alias of "Object" and you will understand it in an instant.
Future Groovy may give "def" an additional meaning in terms of static and dynamic typing. But this is
post Groovy 1.0.
"def" can also replace "void" as the return type in a method definiton.

?

  1. def?dynamic??=??1
  2. dynamic?=?"I?am?a?String?stored?in?a?variable?of?dynamic?type"
  3. int?typed?=?2
  4. typed?=?"I?am?a?String?stored?in?a?variable?of?type?int??"????//?throws?ClassCastException

The assignment of a string,to a variable of type int will fail. A variable typed with "def" allows this.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读