package helloworld import groovy.swing.SwingBuilder import java.awt.BorderLayout import groovy.swing.SwingBuilder import java.awt.BorderLayout as BL import org.junit.*; import java.util.*; class HelloController { enum Day{SUNDAY,MONDAY, TUESDAY, WEDNESDAY,THURSDAY, FRIDAY, SATURDAY} enum Planet {MERCURY(3.303e+23, 2.4397e6), VENUS(4.869e+24, 6.0518e6), EARTH(5.976e+24, 6.37814e6), MARS(6.421e+23, 3.3972e6), JUPITER(1.9e+27,7.1492e7), SATURN(5.688e+26, 6.0268e7), URANUS(8.686e+25, 2.5559e7), NEPTUNE(1.024e+26, 2.4746e7) double mass; double radius; Planet(double mass, double radius){ this.mass = mass; this.radius = radius; } void printMe(){ println "has a mass of and a radius of<BR>"; println "mass:"+mass+", radius:"+radius; //render "printMe:<BR>"; //println "AAAA"; } } def index() { //def grailsVersion = grailsApplication.metadata['app.grails.version']; //render "Hello World!"+grailsVersion; //render grailsVersion; //def mySet = false; //def mySet = Boolean.FALSE; //if(mySet){ //render "true"; //}else{ //render "false"; //} //String str = null; //assert str_ = = null; //render "Hello world it is"+new java.util.Date() + grailsVersion; //String str = null; //assert str1 == null; //Groovy基本语法 //int i = 123 + 45 *67; //render i+"<BR>"; //def x = new java.util.Date(); //render x; //不能直接跟字符串,可能是数据类型问题 //render "<BR>"; //Boolean y = false; //render y+"<BR>"; //List和Maps //List myList = [1776, -1, 33, 99, 0, 928734928763]; //Groovy语法简介 //1 没有类型的java //仅仅要求变量名前使用关键字def(groovy jsr 1开始,在以前的版本中,甚至连def都不需要) def var = "hello world"; render var+"<BR>"; render var.class; //查看变量的类型 //对象输出后面不能跟+号字符链接符号 //作为例外,方法参数和循环变量的声明不需要def //2 不需要的public //3 不需要的语句结束符 //4 字符串链接符 def var2 = "hello world groovy!"; render "<BR>"+var2+"<BR>"; //5 一切皆有对象 def var3 = "hello "+ "world"+ ", groovy!"; render var3+"<BR>"; render var3.class; render "<BR>"; var3 = 1001; render "<BR>重新赋值后的类型:"; render var3.class; //6 循环 //def var4 = "hello "+ "world "+", groovy!"; //def repeat(val){ //for(i=0; i<5; i++){ //render val+"<BR>"; //} //} //repeat(var4); //这样直接循环错误哦 //7 String和Gstring def i=22; def val4="xlc"; //render "this is ${$val4}:${i}"; 不支持了 render "<BR>this is "+val4+":"+i+"<BR>"; //8 范围 def j=22; for (jj in 0..<5){ render jj; } //render "<BR><BR>"; //不成功 //for(jjj in a..<e){ //render jjj; //} //9 默认参数值 /*def repet(val, repeat=3){ for(i in 0..<repeat){ render "this is ...."; } }*/ //这个例子肯定运行不了 //10 集合 //Groovy支持最常见的两个java集合,java.util.Collection和java.util.Map //前面所说的范围实际也是集合的一种java.util.List //(1)Collection //添加 def collect = ["a","b","c"]; collect.add(1); collect<<"come on"; collect[collect.size()]=100.0; //输出 render collect[0]+"<BR>"; render collect[collect.size()-1]+"<BR>"; render collect.size(); for(i=0; i<collect.size(); i++){ render i+":"+collect[i]+"<BR>"; } //groovy支持负索引 render "-1:"+collect[-1]+"<BR>"; //索引其倒数第1个元素 render "-2:"+collect[-2]+"<BR>"; //索引其倒数第2个元素 //collection支持集合运算 collect = collect + 5; render collect[collect.size()-1]+"<BR>"; collect = collect-'a'; render collect[0]; //同样的,你可以往集合中添加另一个集合或删除一个集合 collect = collect-collect[0..4]; //把集合的前5个元素去掉 render collect[0]+"<BR>"; //现在集合中仅有一个元素,即原来的最后一个元素 render collect[-1]+"<BR>"; //也可以用负索引,证明最后一个元素就是第一个元素 //Map def map = ["name":"john", "age":14, "sex":"boy"]; map = map+["weight":25]; map = map+["length":1.27]; map.father = "Keller"; render map['father']+"<BR>"; render map.length+"<BR>"; //11 闭包(Closure) //key, value两个参数用于接受每个元素的键/值 map.each({key,value->render "$key:$value <BR>"}); map.each{render it} //it是一个关键字,代表map集合的每个元素 render "<BR>"; map.each({render it.getKey()+"--->"+it.getValue()}); //除了用于迭代之外,闭包也可以单独定义 def say = { word->render "Hi, $word!<BR>"; } //调用 say('groovy'); say.call("groovy&grails"); //12 类 //(1) 不需要public修饰符 //(2) 不需要类型说明 //(3) 不需要getter/setter方法 //(4) 不需要构造函数 //(5) 不需要return //(6) 不需要()号 //Groovy中方法调查用可以省略()号(构造函数除外),也就是说下面两句是等同的 //person1.setName 'kk'; //preson1.setName ('kk'); //标准java写法 def person1 = new Person(); person1.name = 'kk'; person1.age = 20; render person1; render "<BR>"; def person2 = new Person(["name":'gg', "age":33]); render person2; render "<BR>"; //这样需要要注意我们覆盖了Object的toString方法,因为我们想通过render person1这样的方法简单地打印对象的属性值 //然而toString方法中并没有return一个string,但不用担心,Groovy默认返回方法的最后一行的值 //13 ?运算符 //rs ?.next(); //?在这里是一个条件运算符,如果?前面的对象非null,执行后面的方法,否则什么也不做 //14 可变参数 //等同于java5中的变长参数,首先我们定义一个变长参数的方法sum: //下面的测试没有通过 /*int sum(int... var) { def total = 0; for(i in var){ total += i; } return total; } render sum(1); */ //15 枚举 // enum Day{ SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY} //enum Planet {MERCURY(3.303e+23, 2.4397e6)} //enum Day{SUNDAY,MONDAY, TUESDAY, WEDNESDAY,THURSDAY, FRIDAY, SATURDAY} //这个枚举要定义到函数的外面,也就是类的下面 //然后我们在swith语句中使用他 def today = Day.SATURDAY; render today; switch(today){ case "SATURDAY": render "可以休息啦<BR>"; break; case Day.MONDAY..Day.FRIDAY: render "今天得上班啊<BR>"; break; default: render "这么奇怪<BR>"; } //同java5一样,groovy支持带构造器,属性和方法的enum: //如顶部构造函数 Planet.EARTH.printMe(); //println是只能在命令窗口出现的打印数据 //16 Elvis操作符 //这是三目运算符"?:"的简单形式,三目运算符通常以这种形式出现 def name = null; //String displayName = name !=null ? name : "Unknown"; //String displayName = name ? name : "Unknown"; String displayName = name ?: "Unknown"; render displayName+"<BR>"; //17 动态性 //Groovy所有对象都有一个元类metaClass,我们可以通过metaClass属性访问该元类,通过元类,可以为这个对象增加方法 //下面代码,msg是一个String,通过元类,我们为msg增加了一个String类中所没有方法up: def msg = "Hello"; render msg.metaClass render "<BR>"; //添加元件到没有成功 //String.metaClass.up = { delegate.toUpperCase();} //render msg.up(); //通过元类,我们还可以检索对象所拥有的方法和属性(就像反射) msg.metaClass.methods.each{ render it.name+"<BR>"; } msg.metaClass.properties.each{ render it.name+"<BR>";} //我们可以通过元类判断有没有一个叫up的主应运而生,然后再调用它 if(msg.metaClass.respondsTo(msg, 'up')){ render "ok exists<BR>"; }else{ render "isexists up<BR>"; } if(msg.metaClass.hasProperty(msg, 'bytes')){ render msg.bytes.encodeBase64(); } //18 Groovy swing //好像是生成表单和按钮,但没成功 /*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.pack() frame.show()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.pack() frame.show()*/ } } class Person { def name; def age; //注意方法的类型String,因为我们要覆盖的方法为String类型 String toString(){ "$name,$age"; } }