(Transfer) Groovy's 5 special operators (can be used for Play! The template)

2010-04-09  来源:本站原创  分类:Java  人气:191 

Spread Operator (Expand operator)

Expand the use of a set operator (*.) can be an element of a collection of various elements of the original method of execution from the back of the specified set of values

Groovy code

  1. class Person (
  2. String name
  3. int age
  4. String toString () (
  5. return "$ (name): $ (age)"
  6. )
  7. )
  8. def people = [new Person (name: 'A great', age: 24), new Person (name: 'A 2', age: 18)]
  9. println people *. toString ()
  10. ========================
  11. result:
  12. ["A large: 24", "A 2: 18"]

class Person{
    String name
    int age
    String toString(){
        return "${name}:${age}"
    }
}

def people = [new Person(name:' ADA  ',age:24),new Person(name:' Wife there  ',age:18)]
println people*.toString()

========================

result:

[" ADA  :24", " Wife there  :18"]


Elvis Operator (binary operator)

Groovy as the "non-empty is true", so the original java in the ternary operator can be reduced to binary operator

Groovy code

  1. String name = person.name? Person.name: 'unknown' / / java wording
  2. def name2 = person.name?: "unknown" / / Groovy wording

String name=  person.name?person.name:'unknown'// java Grimm  

def name2= person.name ?: "unknown" // Groovy
  Grimm

Safe Navigation / Dereference Operator (security placeholder)
Security placeholder (?.) Used to avoid null pointer exception

Groovy code

  1. Person user
  2. println user.name / / Throws NullPointerException
  3. / / Null pointer exception in order to avoid the need to add a check, java wording is as follows
  4. if (user! = null) (
  5. println "Java FirstName = $ (user.firstName)"
  6. )
  7. / / Groovy syntax of the written
  8. println "Groovy FirstName = $ (user?. firstName)"

Person user
println user.name// Throws NullPointerException
//  To avoid null pointer exception, you need to add a check  ,java The wording as follows
if (user != null) {
    println "Java FirstName = ${user.firstName}"

}
// Groovy
 Syntax notation
println "Groovy
 FirstName = ${user?.firstName}"

Field Operator

Check the dictionary field operator translated as "field operator," I am not good in terms of understanding the Chinese. So, not translated.

Because Groovy support automatic property getter, but sometimes needs to be a particular need to automatically write a special getter, if sometimes need to call a special getter, sometimes want to call ordinary POJO as the getter, then how do what? Use Groovy's Field Operator can easily solve this problem. Examples are as follows:

Groovy code

  1. class Todo (
  2. String name
  3. def getName () (
  4. println "Getting Name"
  5. name
  6. )
  7. )
  8. def todo = new Todo (name: "Jim")
  9. println todo.name
  10. println todo. @ name
  11. ========
  12. result:
  13. Getting Name
  14. Jim
  15. Jim

class Todo {
    String name
    def getName() {
      println "Getting Name"
      name
    }
}

def todo = new Todo(name: "Jim")
println todo.name
println [email protected]

========
result:
Getting Name
Jim
Jim

Do not use the Field Operator is to write the getter tune

Field Operator is emphasized that the use of getter POJO

Method Closure Operator

We know that closure can be used as a method parameter, but how to make a method as a way of arguments? Method Closure Operator is used to solve the above problems, it allows a method as a closure as a parameter to another method.

Groovy code

  1. def list = ['boss', 'second child', 'youngest']
  2. / / Conventional traverse
  3. list.each (
  4. println it
  5. )
  6. String printName (name) (
  7. println name
  8. )
  9. / / Will printName as the parameters traverse
  10. list.each (this. & printName)

def list = [' The boss ', ' younger son  ',' Three committees  ']
// Regular walks  

list.each{
    println it

}

String printName(name){
    println name
}

// Will printName as an argument to traverse
list.each(this.&printName)
相关文章
  • (Transfer) Groovy's 5 special operators (can be used for Play! The template) 2010-04-09

    Spread Operator (Expand operator) Expand the use of a set operator (*.) can be an element of a collection of various elements of the original method of execution from the back of the specified set of values Groovy code class Person ( String name int

  • Flocking transfer advantages of the new method 2010-02-18

    Keywords: flocking, transfer Flocking transfer advantages of the new method 1. Simple process and does not require technical training, practice what can be successful; 2. Low cost and requires only two ingredients: Flocking Flocking transfer paper an

  • Three kinds of clothing in modern society special printing technology 2010-02-18

    Keyword: Clothing technology printing technology is an important part. Especially when people are all kinds of strange clothing industry of modern printing technology will bring the prospect of a more beautiful, so clothing more closely with the need

  • The difference between thermal transfer and digital printing 2010-02-18

    Keyword: What is Heat Transfer? Why use thermal transfer technology? Heat is produced in a variety of material goods on the pattern of new methods, especially for production of a small variety of personalized and customized products, as well as print

  • MySQL development specifications and practical technology exchange (transfer) 2011-07-12

    Original Address: http://www.nuxnu.com/2011/07/2/MySQL% E5% BC% 80% E5% 8F% 91% E8% A7% 84% E8% 8C% 83% E4% B8% 8E % E5% AE% 9E% E7% 94% A8% E6% 8A% 80% E6% 9C% AF% E4% BA% A4% E6% B5% 81.html Content 1. The development of practical techniques 2.MySQ

  • python template engine in the field of transfer 2010-06-10

    GenShi Features: interpreted template, simply for the xml, streaming processing mechanism, can be embedded python statements and expressions, providing a powerful feature. Evolved from the Kid, more flexible than the Kid, performance is also better t

  • Black Hawk Honker Base Tutorial - 03 - VB6 .0 Programming Basics 2010-12-12

    Notes, just for reference! ! ! -------------------------------------- Blackhawk Blackhawk Honker Honker base base www.3800hk.com tutorial Safety technology professional training base Honker taking a minute to learn more. Let your life more exciting.

  • Black Hawk Base Tutorial - 03 - VB6 .0 Programming Basics 2010-12-12

    Notes, just for reference! ! ! -------------------------------------- Blackhawk Blackhawk Honker Honker base base www.3800hk.com tutorial Safety technology professional training base Honker taking a minute to learn more. Let your life more exciting.

  • Learn JavaScript 2009-05-03

    JavaScript programming book (2nd edition) The first part of basic grammar chapter JavaScript Introduction JavaScript is a small, lightweight, object-oriented, cross-platform scripting language, is the most popular web page programming language client

  • Java Regular 2010-03-29

    Lazy quantifiers:?? +? *? (N,)? (N, m)? Greedy quantifiers:? + * (N,) (n, m) Invasion and occupation of quantifiers:? + + + * + (N,) + (n, m) + English: [\ u4E00-\ u9FA5] 13.9 Pattern class methods So far, we only use the test sample to create a Patt

  • Common SQL statements 2010-03-13

    SQL Category: DDL-Data Definition Language (CREATE, ALTER, DROP, DECLARE) DML-Data Manipulation Language (SELECT, DELETE, UPDATE, INSERT) DCL-Data Control Language (GRANT, REVOKE, COMMIT, ROLLBACK) First, a brief description based on the statement: 1

  • Between different types of printing technology advantages and disadvantages 2010-02-18

    Keywords: printing technology, paste printing, ink printing, transfer printing printing, is the pattern in the textile printing technology on the process. In accordance with pre-printed fabric patterns can be divided into cloth printing, printing and

  • T shirt printing technology of the type and characteristics 2010-02-18

    Keywords: T-shirt printing, technology, types and characteristics of textile printing is printed in the technical design process. In accordance with pre-printed fabric patterns can be divided into cloth printing, printing and garment printing pieces.

  • The basic concepts of print 2010-02-18

    Keywords: printing, the basic concept of printing, is the pattern in the textile printing technology on the process. In accordance with pre-printed fabric patterns can be divided into cloth printing, printing and garment printing pieces. Cloth printi

  • T shirt printing technology 2010-02-18

    Keyword: In accordance with pre-printed fabric patterns can be divided into cloth printing, printing and garment printing pieces. Cloth printing cloth is printed on the whole pattern piece only after cutting; pieces printing is completed garment cutt

  • Issuing bank acquirer acquirer 2010-06-09

    The main functions of the card issuer is all kinds of bank cards issued to the cardholder, and by providing various services related to bank card fee. Through the issuance of bank cards, card issuers access to the cardholder's credit card to pay annu

  • javascript expressions and statements 2010-08-31

    1.javascript total including arithmetic expressions, string expression, relational expression, logical expression. Corresponding to the assignment operator arithmetic operators, string operators, comparison operators, logical operators, bitwise opera

  • Find the file linux command summary 2010-12-14

    Find is a very effective tool that can traverse the entire file system and even the current directory to find certain files or directories. Main options: -Name find files by file name -Perm to find files by file permissions -Prune do not find the spe

  • CL's apply, funcall and its parameters 2011-01-04

    apply / funcall function & rest args + apply can accept multiple parameters, but the last argument must be a list, where each element is a function of the parameters funcall can accept multiple parameters, are the function of the parameters for the l

  • Sphinx Mysql Full-Search Quick Guide 2011-08-26

    Sphinx Mysql Full-Search Quick Guide Directory 1. Sphinx Profile 1.1 What is the full-text search 1.2 Introduction 1.3. Sphinx features 2. Sphinx installation (For MySQL) 2.1. Windows install 2.2. Linux install 3 examples 4. Sphinx configuration 5 ru