gpt4 book ai didi

java - 所包含的匿名内部类使用的函数的参数由 "final"修改。但为什么?

转载 作者:行者123 更新时间:2023-12-01 13:16:23 25 4
gpt4 key购买 nike

public Destination destination(final String dest, final float price) {
return new Destination() {
private int cost;
{
cost = Math.round(price);
if (cost > 100)
System.out.prinltn("Over budget!");
}
private String label = dest;
public String readLabel() { return label; }
};
}

所包含的匿名内部类使用的参数由“final”修饰。但为什么?

最佳答案

关于规则适用于内部类 (JLS 8.1.3) :

Any local variable, formal method parameter or exception handler parameter used but not declared in an inner class must be declared final. Any local variable, used but not declared in an inner class must be definitely assigned before the body of the inner class.

了解更多 here

关于java - 所包含的匿名内部类使用的函数的参数由 "final"修改。但为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22434740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com