What is this "def" I heard of?
Posted zhangjianying
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了What is this "def" I heard of?相关的知识,希望对你有一定的参考价值。
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.
- def dynamic = 1
- dynamic = "I am a String stored in a variable of dynamic type"
- int typed = 2
- 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.
以上是关于What is this "def" I heard of?的主要内容,如果未能解决你的问题,请参考以下文章
[转帖]Warning:detected "cgroupfs" as the Docker cgroup driver. The recommended driver is &qu
what is "interpolation function"
Kendo UI 网格 - throw Error(kendo.format("Cannot call method '0' of 1 before it is initialized&qu
{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported&qu