Groovyb脚本速成教程

本文介绍了 Groovy 编程语言的基本语法,包括屏幕输出、变量定义、类和函数定义等核心概念,并通过实例展示了列表和映射的操作,以及常用的逻辑语句和循环结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要介绍了groovy的常用语法,以便快速上手应用.
 
println “Hello world!”
println “Hello” + “ world!”
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 
 }
}
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")
    }
}
 
 
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
       }
    }
 }
}  
 
 
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")
    }
}
 
 
class HelloWorld {
   
  staticvoid main(args) {
       def s = "1234"
       if(s == "1234")   println"yes,it is 1234"
      
       def n = 1234
       if(n == 123println"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"
    }
}
 
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;
       }
    }
}
 
class HelloWorld {
   
  staticvoid main(args) {
       int n = 10
       while(n){        
           println"n=" + n
           n --
       }
    }
}
class HelloWorld {
   
  staticvoid main(args) {
       def n = [10,20,30]
       for(e in n){        
           println"e=" + e
       }
    }
}
 
 
 
 
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()
       }
    }
}
 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值