groovy入門

来源:互联网 发布:g20 淘宝 编辑:程序博客网 时间:2024/04/29 08:10

直接見代碼.groovy有啥優點就不說了.直接google大神去吧~

 

package test

/**
 * @author Yansheng
 *
 */
public class HelloWorld{
 
 /**
  * 赋值,调用域
  */
 def x;
 def y;
 private float z;

 public static void main(String... args) {
  def hello = new HelloWorld(x:3,y:'yansheng',z:3.4); // 初始化对象,赋值域
  println '------------  invoke domain(调用域) ---------------';
  println 'invoke x: ' + hello.x; // 调用x域
  println 'invoke y: ' + hello.y; // 调用y域
  println 'invoke z: ' + hello.z; // 调用z域
  hello.method('invoke Method'); // 调用方法
  hello.operator(); // 打印操作运算
  hello.testString(); // 测试字符串
  hello.invokeMapping(); // 调用列表,映射 范围
  hello.formatPrint(); // 输出
  hello.flowControl();
  hello.closure();
 }

 /**
  * 调用方法
  */
 public void method(message, msg=3) {
  println message;
 }
 
 /**
  * 操作运算
  */
 public void operator() {
  println '------------  print operator(打印操作符) ---------------';
  print '加法: ';    print 5 + 3 + '/t';    println 5.plus(3);
  print '减法: ';    print 5 - 3 + '/t';    println 5.minus(3);
  print '乘法: ';    print 5 * 3 + '/t';    println 5.multiply(3);
  print '除法: ';    print 5 / 3 + '/t';    print 5.div(3) + '/t';    println 5.intdiv(3)
  print '模法: ';    print 5 % 3 + ' ';    println 5.mod(3);
 }
 
 /**
  * 测试字符串
  */
 public void testString() {
  def greeting = 'Hello Wrold';
  
  println '------------  test String(测试字符串) ---------------';
  def age = 25;
  println 'my age is ${age}';
  println "my age is ${age}";
  println """my age is ${age}""";
  println "my age is /${age}";

  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, 12, 13, 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');
 }
}