Groovy學習筆記-Java 5新特性支援

來源:互聯網
上載者:User

標籤:values   rabl   medium   null   manager   worker   write   hello   short   

1.枚舉enum

enum CoffeeSize{    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;        case CoffeeSize.MEDIUM..CoffeeSize.LARGE:            println ‘Programmer‘            break;        case CoffeeSize.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 received for size MUG:Caffeine IVSHORTSMALLMEDIUMLARGEMUG*/

2.for-each迴圈

def list = [1,3,6,4,9]println ‘傳統for迴圈‘for(int i = 0; i < list.size(); i++){    println i}println ‘實現了 Iterable介面的for迴圈‘for(int i : list){    println i}println ‘不指定類型的for迴圈‘for(i in list){    println i}/*output傳統for迴圈01234實現了 Iterable介面的for迴圈13649不指定類型的for迴圈13649*/

3.變長參數

def receiveVarArgs(int a, int...b){    println "$a and $b"}def receiveArray(int a, int[] b){    println "$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.註解[email protected]

class Worker{    def Work(){println ‘Work‘}    def Analyze(){println ‘Analyze‘}    def WriteReport(){println ‘WriteReport‘}}class Expert{    def Analyze(){println ‘Expert Analyze‘}}class Manager{    @Delegate Expert expert = new Expert()    @Delegate Worker worker = new Worker()}def ironcrow = new Manager()ironcrow.Work()ironcrow.Analyze()ironcrow.WriteReport()/*outputWorkExpert AnalyzeWriteReport*/

5-註解@Lazy:惰性建立

// 非惰性建立class Heavy {    def size = 10    Heavy(){        println("Create Heavy with $size")    }}class AsNeeded {    def value     Heavy heavy1 = new Heavy()     Heavy heavy2 = new Heavy(size: value)    AsNeeded(){        println ‘Created AsNeeded‘    }}class Program {    static void main(String[] args){        def asNeeded = new AsNeeded(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*/
// 惰性建立class AsNeeded {    def value    @Lazy Heavy heavy1 = new Heavy()    @Lazy Heavy heavy2 = new Heavy(size: value)    AsNeeded(){        println ‘Created AsNeeded‘    }}/*outputCreated AsNeededCreate Heavy with 101010Create Heavy with 1010001000*/

[email protected]單例

@Singleton(lazy = true)class TheUnique{    def sayHello(){        println ‘hello‘    }}TheUnique.instance.sayHello()/*outputhello*/

 

Groovy學習筆記-Java 5新特性支援

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.