Groovy   发布时间:2022-04-12  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了groovy basic knowledge大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
简单地说,Groovy 是下一代的java语言,跟java一样,它也运行在 JVM 中。
作为跑在JVM中的另一种语言,groovy语法与 Java 语言的语法很相似。同时,Groovy 抛弃了java烦琐的文法。 同样的语句,使用groovy能在最大限度上减少你的击键次数――这确实是“懒惰程序员们”的福音。
 
(1)
how to run the groovy using eclipse IDE:
1.
download the groovy plug-in for eclipse and put then under the related directory.
2.
create a java project and right-click the project,SELEct 'groovy' then choose 'add groovy nature'. this operation let this java project support the groovy;
3.
aiming to separate the java class and the groovy class,we can create com.java,com.groovy for related classes.
create groovy class:
new-->others-->groovy-->groovy class.
follow the above steps,we can create a groovy class.
4.
run the groovy:
just like to run java class.
 
(2)
examples:
 
package com.test.groovy
import groovy.sql.sql;
public class HelloWorld{
    public static void main(def args){
@R_450_5179@ println "Hello Word !"
@R_450_5179@ def var="Hello world"+"groovy";
@R_450_5179@ println var;
@R_450_5179@ println var.class;
@R_450_5179@ var=100;
@R_450_5179@ println var.class;
@R_450_5179@ String nullValue
//?操作符时刻都非常有用,可以极大地减少条件语句。若不为null,则会执行后面的
@R_450_5179@ nullValue?.toLowerCase()
@R_450_5179@ println "-----------------------------"
@R_450_5179@
@R_450_5179@ def hw=new HelloWorld();
@R_450_5179@ hw.show1("Hello")
@R_450_5179@ hw.show1("world",2)
@R_450_5179@ hw.show2("test",2)
//@R_450_5179@ hw.connectDB(6)
@R_450_5179@
@R_450_5179@ println "-----------闭包,常用于循环------------------"
@R_450_5179@ def list=['a','b','c']
@R_450_5179@ list.each{println it}
@R_450_5179@ def map=['name':'john','age':14,'sex':'boy']
@R_450_5179@ map=map+['weight':25]       //添加john的体重
@R_450_5179@ map.put('length',1.27)      //添加john的身高
@R_450_5179@ map.father='Keller'@R_450_5179@  //添加john的父亲
@R_450_5179@
@R_450_5179@ println map['name']
@R_450_5179@ println map['length']
@R_450_5179@ map.each{key,value-> println "$key:$value"}
@R_450_5179@ println "-----------------------------"
@R_450_5179@
    }
   
    def show1(val,count=3)
    {
@R_450_5179@ for(i in 0..<count) //compare with the later method(show2)
@R_450_5179@ {
@R_450_5179@     println i
@R_450_5179@     println "this is a ${i} test: ${val} !";
@R_450_5179@ }
    }
   
    def show2(val,count)
    {
@R_450_5179@ for(i in 0..count)
@R_450_5179@ {
@R_450_5179@     println "this is a"+i+" test: "+val;
@R_450_5179@ }
    }
   
    def connectDB(id)
    {
@R_450_5179@ def url = "jdbc:Oracle:thin:@10.50.74.117:1521:mssapp"
@R_450_5179@ def driver = "Oracle.jdbc.driver.OracleDriver"  
@R_450_5179@ def user = "dev"  
@R_450_5179@ def password = "dev"
@R_450_5179@
@R_450_5179@ def sql = sql.newInstance(url,user,password,driver)
@R_450_5179@ sql.execute("insert into test_account values('5','account5')")@R_450_5179@     
@R_450_5179@ sql.execute("insert into test_account values('7','account7')")   
@R_450_5179@ sql.execute("insert into test_account values(${iD},'account6')")   
@R_450_5179@ sql.execute("update test_account set ACCOUNTNAME=? WHERE AC_ID=?",["herry Test",id]) 
@R_450_5179@ sql.execute("delete from test_account where AC_ID>4")
@R_450_5179@ def row = sql.firstRow("SELECT COUNT(*) AS totalRECORD FROM test_ACCOUNT")
@R_450_5179@ println row.@R_635_10586@LRECORD
@R_450_5179@
@R_450_5179@ //使用groovy的隐含变量 it(它恰好就是迭代器的实例)
@R_450_5179@ sql.eachRow("SELECT * FROM tEST_ACCOUNT")
@R_450_5179@ {
@R_450_5179@     println it.AC_ID
@R_450_5179@     println it.ACCOUNTNAME
@R_450_5179@ }
@R_450_5179@
    }
}
need lib:  classes12.jar
-------------------------------------------------------------------------------------------
package com.test.groovy
public class PersonInfo{
    //对于javabean各属性认为private(不同于groovy平时的认设置:平时认为publiC)自动生成setter,getter.
    //对于单独的方法,如toShow(),认仍为public
    Integer persionId
    String name
    Double weight
    public String pas;
    def addr;
   
    String toShow()
    {
@R_450_5179@ return " persionId -- ${persionID} \n name -- ${name}"
    }
}
-------------------------------------------------------------------------------------------
package com.java;
import com.test.groovy.HelloWorld;
import com.test.groovy.PersonInfo;
public class TestGroovy {
    public static void main(String[] args) {
@R_450_5179@ HelloWorld hw=new HelloWorld();
@R_450_5179@ hw.show1("tesTing in java");
@R_450_5179@
@R_450_5179@ PersonInfo pi=new PersonInfo();
@R_450_5179@ pi.setPersionId(123);
@R_450_5179@ pi.setName("tom");
@R_450_5179@ pi.setWeight(120.0);
@R_450_5179@ System.out.println(pi.getName());
@R_450_5179@ System.out.println(pi.getWeight());
@R_450_5179@ System.out.println(pi.toShow());
    }
   
    public int plus(int i,int y)
    {
@R_450_5179@ return i+y;
    }
}
 
references:
基础:
与 java整合:
实战 Groovy: 用 Groovy 进行 JDBC 编程
http://www.ibm.com/developerworks/cn/java/j-pg01115.html
与 ssh等的结合.

大佬总结

以上是大佬教程为你收集整理的groovy basic knowledge全部内容,希望文章能够帮你解决groovy basic knowledge所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。