加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Groovy高级特性(一)

发布时间:2020-12-14 16:43:22 所属栏目:大数据 来源:网络整理
导读:参考文献: 《The document of Groovy》 1.Map类型的值引用 def key = "name" map =[key:"david"] - assert map.key == "david" map = [(key):"david"] - assert map.name == "david" 2.类的property调用 class MyObject { ????[static] String name ????def

参考文献: 《The document of Groovy》

1.Map类型的值引用

def key = "name"

map =[key:"david"]

-> assert map.key == "david"

map = [(key):"david"]

-> assert map.name == "david"


2.类的property调用

class MyObject {

????[static] String name

????def getName() {

????????"<<<"+name+">>>"

????}

}

obj = new MyObject(name:"david")

->assert obj.name == "<<<david>>>"

如果要直接引用对象属性:

->assert obj.@name == "david"


3.函数指针操作:

def str = "cdw"

def fun = str.&toUpperCase

assert fun() == str.toUpperCase()

注:函数指针并不指向实际地址,而是指向一个method name索引

def doSomething(String str) {println "do String"}

def doSomething(int i) {println "do Integer"}

def fun = this.&doSomething

assert fun("david") == "do String"

assert fun(1) == "do Integer"

4.属性分割符号.*

users = [

new User(name:"david",age:10),

new User(name:"lily",age:12),]

assert users.*name == ["david","lily"]

5.[]运算符重载

class User {

????long id

????String name

? ? ?def getAt(int i) {

? ? ? ? ? ?switch (i) {

????????????case 0: return id

????????????case 1: return name

????????}

????}

? ? ?def putAt(int i,obj) {...}

}

u = new User(id:100,name:"david")

assert u[0] == 100

assert u[1] == "david"

6.as 运算符:

Integer a = 123

String str = a as String

as操作符需要复写Object的asType方法

def asType(Class clazz) {

????if (clazz == String) {

????????return new String(this.toString())

????}

}

没有构造器的对象可以指定为各种的类型

class?Callback?{
????????void?callback()?{println?"call?callback!!"}
}

interface?IInterfacer?{}

callback?=?new?Callback()
i?=?callback?as?IInterfacer

assert i instanceof IInterfacer

属性类型转换:

class Person {

?????String name

?????int age

}

p = ["david",12] as Person

or Person p = ["david",12]


7.()括号运算符

()需要复写call方法

class Obj {

????int call(int x) {

????????9+x

????}

}

obj = new Obj()

int n = obj.call(1)

assert n == 10

assert obj(1) == 10

8.如果传入的参数是Map类型可以使用列举的方式

?def mapFunc(Map map) {

?????map.each {

??????????println it.key+"->"+it.value

?????}

}

mapFunc(name:"david",age:19)

or mapFunc name:"david",age:19

9.class mixin操作

class A {def methodA() {println "call A"}}

class B {def methodB() {println "call B"}}

A.metaClass.mixin ?B

def o = new A()

assert o.methodB() == 'call B'

assert !(o instanceof B)

注:o.class实际上市一个Proxy

10.Closure返回值:

通过泛型确定Closure的返回值类型

Closure<Boolean> c = {->false}

?11.科里化闭包参数(给闭包参数指定默认值)

def testCurry = {int age,String name->println "$name is $age"}

左侧赋值:

testCurry.curry(12)

assert testCurry("david") == "david is 12"

右侧赋值:

testCurry.rcurry("lily")

assert testCurry(9) == "david is 9"

索引赋值:

testCurry.ncurry(0,123)

assert testCurry("sanny") == "sanny?is 123"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读