Groovy 几个例子

Bike.groovy

 

package test.groovy;
class Bike {
 String manufacturer
 String model
 Integer frame
 String serialNo
 Double weight
 String status
 BigDecimal cost
 
 public void setCost(BigDecimal newCost) {
  cost = newCost.setScale(3, BigDecimal.ROUND_HALF_UP)
 }
 
 public String toString() {
//  return "Bike:\n" +
//         "manufacturer: ${manufacturer}\n" +
//         "model: ${model}\n" +
//         "frame: ${frame}\n" +
//         "serialNo: ${serialNo}";
  
  return """Bike:\n""" +
  """manufacturer --  ${manufacturer}
 model -- ${model}
  frame -- ${frame}
  serialNo -- ${serialNo}
  weight -- ${weight}
  status -- ${status}
  cost -- ${cost}
  """
 }
}

 

 

 

BikeTest.groovy

package test.groovy;

import groovy.util.GroovyTestCase;

public class BikeTest extends GroovyTestCase {
 void testBike() {
  // Groovy way to initialize a new object
  def b = new Bike(manufacturer:"Shimano", model:"Roadmaster")

  System.out.println(b.getManufacturer());
  System.out.println(b.getModel());

  System.out.println("-------------------");
  
  System.out.println(b.manufacturer);
  System.out.println(b.model);

  System.out.println(b);
  
  // explicitly call the default accessors
  assert b.getManufacturer() == "Shimano"
  assert b.getModel() == "Roadmaster"
  
  // Groovier way to invoke accessors
  assert b.model == "Roadmaster"
  assert b.manufacturer == "Shimano"
 }
}

 

 

ArrayListRentABike.groovy

 

package test.groovy;

public class ArrayListRentABike {
 String storeName
 List<Bike> bikes = []
   
 public ArrayListRentABike() {
  // add new instances of Bike using Groovy's initializer syntax
  bikes << new Bike(manufacturer: "Shimano",
           model: "Roadmaster",
           frame: 20,
           serialNo: "11111",
           weight: 15,
           status: "Fair")
  bikes << new Bike(manufacturer: "Cannondale",
        model: "F2000",
        frame: 18,
        serialNo: "22222",
        weight: 12,
        status: "Excellent")
  bikes << new Bike(manufacturer:"Trek",
        model: "6000",
        frame: 19,
        serialNo: "33333",
        weight: 12.4, 
        status: "Fair")
 }
 
 // Groovy returns the last value if no return statement is specified
 public String toString() {
  "Store Name:=" + storeName + "\n" +
  "bikes: ${bikes}";
 }
 
 // Find a bike by the serial number
 def getBike(serialNo) {
  bikes.find{
   it.serialNo == serialNo
  }
 }
}

 

 

ArrayListRentABikeTest.groovy

 

package test.groovy;

import groovy.util.GroovyTestCase;

public class ArrayListRentABikeTest extends GroovyTestCase {
 public void testGetBike() {
  ArrayListRentABike list = new ArrayListRentABike();
  System.out.println(list);
  
  Bike bike = list.getBike("11111");
  System.out.println(bike);
 }
}

 

CommandLineView.groovy

 

package test.groovy;

public class CommandLineView {
 def rentaBike  // no interface or concrete type required, duck typing in action
 
 def printAllBikes() {
  println rentaBike
  rentaBike.bikes.each{
   println it
  }  // no iterators or casting
 }
}

 

CommandLineViewSpringTest.groovy

 

package test.groovy;

import groovy.util.GroovyTestCase;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CommandLineViewSpringTest extends GroovyTestCase {
 public void testPrintAllBikes() {
  // Create a Spring application context object
  ClassPathXmlApplicationContext ctx =
  new ClassPathXmlApplicationContext("test/groovy/RentABike-context.xml");
  
  // retrieve an object from Spring and cast to a specific type
  CommandLineView clv = (CommandLineView) ctx.getBean("commandLineView");
  
  clv.printAllBikes();
 }
}

 

RentABike-context.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx
    xmlns:context="http://www.springframework.org/schema/context
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
    http://www.springframework.org/schema/jee   
    http://www.springframework.org/schema/jee/spring-jee-2.5.xsd   
    http://www.springframework.org/schema/context   
    http://www.springframework.org/schema/context/spring-context-2.5.xsd"  
    default-autowire="byName" 
    > 

 <bean id="rentaBike"
    class="test.groovy.ArrayListRentABike">
        <property name="storeName">
         <value>"Bruce's Bikes (spring bean)"</value>
        </property>
    </bean>
    <bean id="commandLineView"
       class="test.groovy.CommandLineView">
        <property name="rentaBike">
         <ref bean="rentaBike"/>
        </property>
    </bean>
</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值