Groovy最终意味着什么? [英] what does final mean in Groovy

查看:128
本文介绍了Groovy最终意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您在Groovy控制台中运行以下代码,它会打印出8

  class F {

private final Integer val = 2

def set(v){val = v}

def print(){println val}
}

def f = new F()
f.set(8)
f.print()

在Java中,这段代码不会编译,因为在构造函数运行后您不能分配 final 引用。我知道对于属性, final 表示该属性不能在该类的外部更改,但是标记私有字段的含义是什么最后



谢谢,
Don

解决方案

看起来这可能是一个Groovy错误:



我不认为在初始化后应该赋值 val p>

If you run the following code in the Groovy console it prints "8"

class F {

  private final Integer val = 2

  def set(v) {val = v}

  def print() {println val}
}

def f = new F()
f.set(8)
f.print()

In Java this code wouldn't compile because you can't assign a final reference after the constructor has run. I know that for properties, final indicates that the property can't be changed outside the class, but what does it mean to mark a private field final?

Thanks, Don

解决方案

It looks like this might be a Groovy bug:

I wouldn't think that val should be assignable after initialization.

这篇关于Groovy最终意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆