加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Groovy学习笔记之入门一

发布时间:2020-12-14 16:50:09 所属栏目:大数据 来源:网络整理
导读:一.搭建环境 ? 1.下载groovy插件,地址: http://dist.codehaus.org/groovy/distributions/update/GroovyEclipse.zip ? 2.安装插件? ? 解压,把目录features和plugins 拷贝到G:eclipseplug-ingroovyeclipse下。? ? 在G:eclipselinks下创建groovyPlug

一.搭建环境?
1.下载groovy插件,地址:http://dist.codehaus.org/groovy/distributions/update/GroovyEclipse.zip?
2.安装插件?
? 解压,把目录features和plugins 拷贝到G:eclipseplug-ingroovyeclipse下。?
? 在G:eclipselinks下创建groovyPlugin_links.txt,内容为:path=G:eclipseplug-ingroovy?
3.下载groovy,http://groovy.codehaus.org/Download。我下载的是Windows-Installer。目前最新的是Groovy 1.6。?
4.完毕?
二.第一个helloworld?
创建 Groovy 工程?
1.选择 File -> New ->Project?
2.选择 Java Project 并按下 next?
3.在 Project Name 中输入 GroovyJava?
4.在 Project Layout 中选择 Create separate source and output folders 并按下 Finish?
5.在 Package Explorer 寻找新创建的工程,右键选择 Groovy -> Add Groovy Nature?
到目前为止创建的工程中会有一个 src 目录,一个 bin-groovy 目录和几个库文件。 bin 目录被 eclipse 隐藏起来。你需要将 bin-groovy 目录作为默认输出目录连接到 src 目录使得调试器能知道源代码和类文件的关联。?
6. 在 Package Explorer 中,右击 GroovyJava 工程,选择 Build Path ->Configure Build Path?
7.点击 Browse 按钮来改变 Default Output Folder ,从 bin 到 bin-groovy ,按下 ok 。?
在 src 中右键创建 groovy class ,并输入例子?

public class GTest{

???????? static void main(args) {

???????????????? println "helloword"

???????? }

}
运行即可.?

?

参考连接:http://www.voidcn.com/article/p-cyrbvzmv-bkz.html

http://www.cnblogs.com/kevincollins/archive/2009/03/25/1505681.html

?

println "simple test 中国"
def var1="hello world"
println var1
println var1.class

def var="hello "+
"world"+
",groovy!"
println var;
println var.class;
var=1001
println var.class

def var2="hello "+
"world"+
",groovy!"
def repeat(val2){
     for(i = 0; i < 5; i++){
      println val2
      println "This is ${i}:${val2}"
     }
}
repeat(var2)
//collect
def collect=["a","b","c"];
collect.add(1);
collect<<"come on";
collect[collect.size()]=100.0
for(j in 0..collect.size()-1)
{
	println collect[j]
}
//map
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['father']       //通过key作为下标索引
println map.length          //通过key作为成员名索引
//闭包
//key,value两个参数用于接受每个元素的键/值
map.each({key,value->println "$key:$value"})
map.each{println it}     //it是一个关键字,代表map集合的每个元素
map.each({ println it.getKey()+"-->"+it.getValue()})
//单独定义闭包
def say={word->println "Hi,$word!"}

say('groovy')
say.call('groovy&grails 坦克世界')

//import groovy.Person;

def person1=new Person()
person1.name='kk,你好'
person1.age=20
println person1

def person2=new Person(['name':'gg','age':22]) //[]号可以省略
println person2
//动态性

def msg = "Hello!"
println msg.metaClass

//在元类中添加up方法
String.metaClass.up = {  delegate.toUpperCase() }
println msg.up()
//打印元类的所有方法
//msg.metaClass.methods.each { println it.name }
//打印元类的所有属性
//msg.metaClass.properties.each { println it.name }

if (msg.metaClass.respondsTo(msg,'up')) {
    println msg.toUpperCase()
}
//当然,也可以推断它有没有一个叫bytes的属性:
if (msg.metaClass.hasProperty(msg,'bytes')) {
    println msg.bytes.encodeBase64()
}  
       
public class Person{
	 def name
	 def age
	 String toString(){//注意方法的类型String,因为我们要覆盖的方法为String类型
	     "$name,$age"
	 }
}
import groovy.swing.SwingBuilder
import java.awt.BorderLayout
import groovy.swing.SwingBuilder
import java.awt.BorderLayout as BL
def swing = new SwingBuilder()
count = 0
def textlabel
def frame = swing.frame(title:'Frame',size:[300,300]) {
	borderLayout()
	textlabel = label(text:"Clicked ${count} time(s).",constraints: BL.NORTH)
	button(text:'Click Me',actionPerformed: {
				count++; textlabel.text =
				"Clicked ${count} time(s)."; println "clicked"
			},constraints:BorderLayout.SOUTH)
}
frame.setDefaultCloSEOperation(3);
frame.setSize(400,500);
frame.setLocationRelativeTo(null)
frame.pack()
frame.show()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读