[英]Groovy: Access class.value without calling getValue()
I can't find information about this.我找不到这方面的信息。 I wrote a DataType class and want to return the value property as the default
我写了一个 DataType 类,并希望将 value 属性作为默认值返回
MyInteger{
Integer value
MyInteger(Integer iv)
{
this.value = iv
}
}
How can I get the value without calling getValue() ?如何在不调用 getValue() 的情况下获取值?
MyInteger i = new MyInteger(5)
print i.value //works
print i.getValue() //works
print i //this is what I want to achieve
Integer realInt = i //or more specific this
The Standard Integer is capable of that, but how?标准整数能够做到这一点,但如何做到这一点? Thanks for any hint!
感谢您的任何提示!
Not sure what was your intent, but a "new integer" can be crafted and used like so:不确定您的意图是什么,但是可以像这样制作和使用“新整数”:
@groovy.transform.TupleConstructor()
class MyInteger extends Number {
Integer value
@Override
int intValue() {
value.intValue()
}
@Override
long longValue() {
value.longValue()
}
@Override
float floatValue() {
value.floatValue()
}
@Override
double doubleValue() {
value.doubleValue()
}
}
def my = new MyInteger(4)
assert my.value == 4
assert my.getValue() == 4
int integer = (int)my
assert integer == 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.