Groovy动态语言 运行在JVM中的动态语言 可以方便的处理业务逻辑变化大的业务

源代码在线查看: casttest.groovy

软件大小: 1644 K
上传用户: maple_78
关键词: Groovy JVM 动态语言 运行
下载地址: 免注册下载 普通下载 VIP

相关代码

				class CastTest extends GroovyTestCase {
				
				    Short b = 1
				    
				    void testCast() {
				        def x = (Short) 5
				
				        println("Cast Integer to ${x} with type ${x.class}")
				        
				        assert x.class == Short
				        
				        methodWithShort(x)
				    }
				    
				    void testImplicitCast() {
				        Short x = 6
				        
				        println("Created ${x} with type ${x.class}")
				        
				        assert x.class == Short , "Type is ${x.class}"
				        
						methodWithShort(x)
				        
				        x = 7
				        
				        println("Updated ${x} with type ${x.class}")
				        
				        assert x.class == Short , "Type is ${x.class}"
				    }
				
				    void testImplicitCastOfField() {
				
				        println("Field is ${b} with type ${b.class}")
				        
				        assert b.class == Short , "Type is ${b.class}"
				        
				        b = 5
				        
				        println("Updated field ${b} with type ${b.class}")
				 
				        assert b.class == Short , "Type is ${b.class}"
				    }
				    
				    void testIntCast() {
				        def i = (Integer) 'x'
				        
				        assert i instanceof Integer
				    }
				    
				    void testCharCompare() {
				        def i = (Integer) 'x'
				        def c = 'x'
				        
				        assert i == c
				        assert i =='x'
				        assert c == 'x'
						assert i == i
						assert c == c
				
				        assert 'x' == 'x'
				        assert 'x' == c
				        assert 'x' == i
				    }
				    
				    void testCharCast() {
				        def c = (Character) 'x'
				        
				        assert c instanceof Character
				        
				        c = (Character)10
				        
				        assert c instanceof Character
				    }
				    
				    void methodWithShort(Short s) {
				        println("Called with ${s} with type ${s.class}")
				        assert s.class == Short
				    }
				    
				    void methodWithChar(Character x) {
				        println("Called with ${x} with type ${s.class}")
				        
				        def text = "text"
				        def idx = text.indexOf(x)
				        
				        assert idx == 2
				    }
				    // br
				    void testPrimitiveCasting() {
				        def d = 1.23
				        def i1 = (int)d
				        def i2 = (Integer)d
				        assert i1.class.name == 'java.lang.Integer'
				        assert i2.class.name == 'java.lang.Integer'
				
				        def ch = (char) i1
				        assert ch.class.name == 'java.lang.Character'
				
				        def dd = (double)d
				        assert dd.class.name == 'java.lang.Double'
				
				    }
				
				}
							

相关资源