Directory (?) [+]
In the past six months have been writing groovy, basically is in the actual combat learning, from Java to turn, also has no problem, after all, the foundation, the architecture has not changed.
Groovy's powerful features, Closure,metaclass have tried, Closure basically is when the method or callback interface use, then advanced usage is useless, no special experience what benefits, when callback interface is the province of the definition of interface, concise some Metaclass There are special for a module to do some metaclass, and Gorm similar, to some class added Save,update,delete and other methods, the difference is that Gorm is stored in the database, I do the module is called RESTful Service , but also good, if there is a need to change the class and to give the class Add method, or is very applicable. But in the vast majority of cases, these advantages can not be reflected, to the current position, or to groovy some of the syntax is impressive, summed up some common syntax: 1.List, and map initialization
This uses the most, the original Java time, do not want to create a new list or map, and then add element one in, extra cumbersome. Groovy is a lot more concise in syntax.
Define a list: [Java] view plain copy list intlist = [1,3,5,7,9] List stringlist = [' A ', ' B ', ']
Define a map: [Java] view plain copy map map = [a:4,b: ' d ']
When defining a Map, for key, like the example above, there is no special indication that all keys are of type string, the key value is ' a ', ' B ', the above example is equivalent to [Java] view plain copy map map = [' A ': 4, ' B ': ' d ']
At some point, the key of the MAP may be a variable that exists, so at this point, it is necessary to hold the key in parentheses, such as [Java] view plain copy String a = ' I am Key ' map map = [(a): ' I A M Value ']
You can also define an empty List or map [Java] view plain copy list empty = [] Map empty = [:]
What you need to know is that the new list instance in this way is the Arraylist,map instance is the Linkedhashmap 2. $ two operator?:
Java inherits the C question-mark expression, and the two-tuple operator is a more streamlined question mark expression. form: [Java] view plain copy def result = a?: b
If the condition is true, that is, a is true or a has a value, the result is a, otherwise the result is B 3. Security Placeholder
This is useful to avoid a lot of nullpointerexception, but you can't abuse [Java] view plain copy def result = obj?. Property
In the code, obj is an object, the property is a familiar object, the meaning of this line of code, if obj is not NULL, it will return the value of the properties attribute, if obj is null, this will directly return NULL. The statement can be strung down [Java] view plain copy def result = a?. B?. C?. D...
4. Field Operations
By default, all fields in the class, groovy will help to generate a Get method, according to groovy Bean's standard. Outside of the class, even if you use the property name instead of the Get method to fetch the value, you get the value by the Get method. What if you want to take the property value directly? Through the field operator: [Java] view plain Copy Class A {String B} A A = new A () A.B//Get value by the Get Method A.getb ()//Pass Over get method take value a.@b//Direct Take value
5.GString
In Java there is a new String,groovy in the gstring. The syntax of groovy is that if you enclose a string in single quotation marks, it is gstring if it is enclosed in double quotes. So groovy can't directly define char in the original java.
String [Java] view Plain copy string s = ' This is a string '
Gstring [Java] view Plain