作为跑在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){
println "Hello Word !"
def var="Hello world"+"groovy";
println var;
println var.class;
var=100;
println var.class;
String nullValue
//? *** 作符时刻都非常有用,可以极大地减少条件语句。若不为null,则会执行后面的
nullValue?.tolowerCase()
println "-----------------------------"
def hw=new HelloWorld();
hw.show1("hello")
hw.show1("world",2)
hw.show2("test",2)
// hw.connectDB(6)
println "-----------闭包,常用于循环------------------"
def List=['a','b','c']
List.each{println it} def map=['name':'john','age':14,'sex':'boy']
map=map+['weight':25] //添加john的体重
map.put('length',1.27) //添加john的身高
map.father='Keller' //添加john的父亲
println map['name']
println map['length']
map.each{key,value-> println "$key:$value"} println "-----------------------------"
}
def show1(val,count=3)
{
for(i in 0..<count) //compare with the later method(show2)
{
println i
println "this is a ${i} test: ${val} !";
}
}
def show2(val,count)
{
for(i in 0..count)
{
println "this is a"+i+" test: "+val;
}
}
def connectDB(ID)
{
def url = "jdbc:oracle:thin:@10.50.74.117:1521:mssapp"
def driver = "oracle.jdbc.driver.OracleDriver"
def user = "dev"
def password = "dev"
def sql = sql.newInstance(url,user,password,driver)
sql.execute("insert into test_account values('5','account5')")
sql.execute("insert into test_account values('7','account7')")
sql.execute("insert into test_account values(${ID},'account6')")
sql.execute("update test_account set ACCOUNTname=? WHERE AC_ID=?",["herry Test",ID])
sql.execute("delete from test_account where AC_ID>4")
def row = sql.firstRow("SELECT COUNT(*) AS TOTALRECORD FROM TEST_ACCOUNT")
println row.TOTALRECORD
//使用groovy的隐含变量 it(它恰好就是迭代器的实例)
sql.eachRow("SELECT * FROM TEST_ACCOUNT")
{
println it.AC_ID
println it.ACCOUNTname
}
}
} 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()
{
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) {
HelloWorld hw=new HelloWorld();
hw.show1("testing in java");
PersonInfo pi=new PersonInfo();
pi.setPersionID(123);
pi.setname("tom");
pi.setWeight(120.0);
System.out.println(pi.getname());
System.out.println(pi.getWeight());
System.out.println(pi.toShow());
}
public int plus(int i,int y)
{
return i+y;
} } references: 基础: http://blog.csdn.net/kmyhy/archive/2009/05/19/4200563.aspx 与 java整合: http://sheng8407-sina-com.javaeye.com/blog/368079 实战 Groovy: 用 Groovy 进行 JDBC 编程
http://www.ibm.com/developerworks/cn/java/j-pg01115.html 用 Groovy 减少代码冗余
http://www.ibm.com/developerworks/cn/java/j-pg09196.html 与 ssh等的结合. 总结
以上是内存溢出为你收集整理的groovy basic knowledge全部内容,希望文章能够帮你解决groovy basic knowledge所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)