Groovy Learning Note-java 5 new features support

Source: Internet
Author: User

1. Enumerate enum

enumcoffeesize{Short, SMALL, MEDIUM, LARGE, mug}def ordercoffee (size) {print"Coffee order received for size $size:"Switch(size) { Case[Coffeesize.short, Coffeesize.small]: println' Conscious ' Break;  CaseCoffeesize.medium. CoffeeSize.LARGE:println' Programmer ' Break;  CaseCoffeeSize.MUG:println' Caffeine IV ' Break; }}ordercoffee (Coffeesize.small) ordercoffee (coffeesize.large) ordercoffee (coffeesize.mug) for(Size in coffeesize.values ()) {println size}/*Outputcoffee Order received for size Small:consciouscoffee order received for size Large:programmercoffee order Receiv Ed for size Mug:caffeine Ivshortsmallmediumlargemug*/

2.for-each Cycle

def list = [1,3,6,4,9' traditional for loop 'for (int i = 0; i < list.size (); i++) {    ' implements Itera ble interface for loop ' for(int  i:list) {    ' does not specify a type for loop 'for(i in list) {    println i}/*output legacy for loop 01234 implements the Iterable interface for loop 13649 does not specify a type for loop 13649* /

3. Variable length parameters

def receivevarargs (intint... b) {    "$a and $b"}def receivearray (int int [] b) {    "$a and $b"}receivevarargs (1,2,3,4,5) receivearray (1,2,3,4,5) /* OUTPUT1 and [2, 3, 4, 5]1 and [2, 3, 4, 5] */

4. Annotations [email protected]

classworker{def work () {println' Work '} def Analyze () {println' Analyze '} def writereport () {println' Writereport '}}classexpert{def Analyze () {println' Expert Analyze '}}classmanager{@Delegate expert expert=NewExpert () @Delegate worker worker=NewWorker ()}def Ironcrow=NewManager () Ironcrow. Work () Ironcrow. Analyze () Ironcrow. Writereport ()/*Outputworkexpert Analyzewritereport*/

5-Annotation @lazy: Lazy creation

//non-inert creationclassHeavy {def size= 10Heavy () {println ("Create Heavy with $size")    }}classasneeded {def value Heavy heavy1=NewHeavy () Heavy heavy2=NewHeavy (Size:value) asneeded () {println' Created asneeded '    }}classProgram {Static voidMain (string[] args) {def asneeded=NewAsneeded (value:1000) println asNeeded.heavy1.size println asNeeded.heavy1.size println asNeeded.heavy2.size Println AsNeeded.heavy2.size}}/*outputcreate Heavy with 10Create Heavy with 10Created asneeded1010nullnull*/
// Lazy Creation class asneeded {    def value    new  Heavy ()    new  Heavy (size:value)    asneeded () {        ' Created asneeded '    }}/*outputcreated asneededcreate Heavy with 101010Create Heavy with 1010001000* /

[Email protected] single case

true )class  theunique{    def sayHello () {        ' hello '    }} TheUnique.instance.sayHello ()/*outputhello* /

Groovy Learning Note-java 5 new features support

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.