• Groovyb简单教程


    1 内容简介

    主要介绍了groovy的常用语法,以便快速上手应用.

    2 语法介绍

    2.1 屏幕输出

    println “Hello world!”

    println “Hello” + “ world!”

    2.2 变量定义

    class HelloWorld {

    staticvoid main(args) {

    def myString = new String("test String")

    def myObject = new Object()

    int myInt = 8

    myObject = 3

    println"myString=" + myString

    println"myObject=" + myObject

    println"myInt=" + myInt

    }

    }

    2.3 类定义,函数定义

    class HelloWorld {

    defvoidtest(){

    println"this is test func"

    }

    def String getString(String input){

    println"input=" + input

    return"hello," + input

    }

    staticvoid main(args) {

    def hw = new HelloWorld()

    hw.test()

    println hw.getString("Liuyou")

    }

    }

    2.4 list用法

    class HelloWorld {

    staticvoid main(args) {

    //ArrayList可以动态增加大小

    def arrayList = new ArrayList()

    arrayList[0] = "dog"

    arrayList[1] = "cat"

    arrayList[2] = "bird"

    for(l in arrayList){

    println l + " "

    }

    //list,不可以动态增加大小

    def list = [ "dog", "cat", "bird" ]

    for(l in list){

    println l + " "

    }

    //list array

    def lists = [[ "liuyou", "22", "M"],["liudehua", "33", "M"]]

    for(l in lists){

    println l[0] + "-" + l[1] + "-" + l[2]

    }

    for(l in lists){

    for(e in l){

    println e

    }

    }

    }

    }

    2.5 map用法

    class HelloWorld {

    staticvoid main(args) {

    //显示定义map

    def map = new HashMap()

    map.put("ID", 12345)

    map.put("name", "Liuyou")

    map.put("email", "you.liu@alcatel-sbell.com.cn")

    println map.get("ID") + "/" + map.get("name") + "/" + map.get("email")

    //隐示定义map

    def map2 = ["email":"you.liu@alcatel-sbell.com.cn", "name":"Liuyou", "ID":12345]

    println map.get("ID") + "/" + map.get("name") + "/" + map.get("email")

    }

    }

    2.6 逻辑语句

    2.6.1 if…else…

    class HelloWorld {

    staticvoid main(args) {

    def s = "1234"

    if(s == "1234") println"yes,it is 1234"

    def n = 1234

    if(n == 123) println"yes, n is 123"

    elseif(n == 12) println"yes, n is 12"

    elseif(n == 1234) println"yes, n is 1234"

    elseprintln"yes, n is null"

    }

    }

    2.6.2 switch…case…

    class HelloWorld {

    staticvoid main(args) {

    def s = "1234"

    switch(s){

    case"1":

    println"1";

    break;

    case"2":

    println"2";

    break;

    case"1234":

    println"1234"

    break;

    default:

    println"default"

    break;

    }

    }

    }

    2.6.3 while循环

    class HelloWorld {

    staticvoid main(args) {

    int n = 10

    while(n){

    println"n=" + n

    n --

    }

    }

    }

    2.6.4 for循环

    class HelloWorld {

    staticvoid main(args) {

    def n = [10,20,30]

    for(e in n){

    println"e=" + e

    }

    }

    }

    2.7 try…catch…异常语句

    class HelloWorld {

    defvoid testException(){

    try{

    def n = 0;

    def m = 2;

    def l = m/n

    }catch(Exception e){

    println e.toString()

    }

    }

    defvoid testThrow(){

    thrownew java.lang.ArithmeticException()

    }

    staticvoid main(args) {

    def hw = new HelloWorld()

    //除法零异常

    hw.testException()

    //主动抛出异常

    try{

    hw.testThrow()

    }catch(ArithmeticException e){

    println e.toString()

    }

    }

    }

  • 相关阅读:
    百鸡问题-小记
    vs2005下opengl(glut)的配置记录
    GSL+DevC++使用
    VS2005下使用GSL-1.15小结
    GSL--GNU Scientific Library 小记
    Window下设置Octave
    0欧姆电阻和磁珠的作用
    关于接地:数字地、模拟地、信号地、交流地、直流地、屏蔽地、浮地,特别经典哦
    altium designer设置不会产生.pcbpreview、.schpreview等的垃圾文件
    【剑指Offer面试题】 九度OJ1516:调整数组顺序使奇数位于偶数前面
  • 原文地址:https://www.cnblogs.com/liuyou/p/2230633.html
Copyright © 2020-2023  润新知