groovy入門
直接見代碼.groovy有啥優點就不說了.直接google大神去吧~ ? package test /** ?public static void main(String... args) { ?/** ??println '------------? test String Index(测试字符串定位) ---------------';??println greeting[4];??println(greeting[-1]);??println(greeting[1..2]);??println(greeting[1..<3]);??println(greeting[4..2]);??println(greeting[4,1,6]);????println '------------? test String Operater(测试字符串操作) ---------------';??println 'Hello' * 3;??println greeting - 'o Wrold';??println greeting.size();??println greeting.length();??println greeting.count('o');??println greeting.contains('ell');?}??public void invokeMapping() {??println '------------? invoke List(调用数组) ---------------';??def numbers = [11,12,13,14];??println numbers[0];??println numbers[-1];??println numbers.getAt(0..2);??println numbers + [15,16];????println '------------? invoke Map(调用映射) ---------------';??def map = ['Ken':'Barclay','John':'Savage'];??println map['Ken'];??map.put('Bob',2713);??println map.get('Bob',444);??println map.keySet();??println map.size();????println '------------? invoke rang(调用范围) ---------------';??def between = 1990..1999;??println between;??println between.size();??println between.get(0);?}??public void formatPrint() {??println '------------? format print(格式化输入输出) ---------------';??printf('My name s ken /n',[]);??def a = 10;??def b = 15;??printf('the sum of %d and %d is %d /n',[a,b,a + b]);??print "please enter your name: ";??//def name = System.in.readLine()?// 会报错????def name = new BufferedReader( new InputStreamReader(System.in) ).readLine()??println "my name is ${name}";?}??public void flowControl() {??println '------------? flow control(流程控制) ---------------';??def LIMIT = 10;??for (count in 1..LIMIT){???println "count: ${count}";??}??for (count in [11,14]){???println "count: ${count}";??}????/*??var score;??switch(score) {???case 70..100:????break;???case 'one':????break;???case [21,22,23,24]:????break;??}??*/?}??public void closure() {??println '------------? closure(闭包) ---------------';??println '闭包语法{comma-separated-formal-parameter-list -> statement-list}';??def clos = {println 'This is closure'};??clos.call();??clos = {param -> println "This is ${param}"};??clos.call('closure');??clos('closure');??clos = {println "This is ${it}"};?// it是隐函数.只能是it??clos.call('closure');?}} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |