方法签名中的 Final 关键字
可能的重复:
接口方法中的最终参数 - 有什么意义?
在尝试时为了尝试一些事情,我遇到了 此页面。
interface B {
public int something(final int a);
}
abstract class C {
public int other(final int b);
}
class A extends C implements B {
public int something(int a) {
return a++;
}
public int other(int b) {
return b++
}
}
为什么这样的功能是可能的?我不知道为什么可以通过覆盖该方法将最终参数变成非最终参数。为什么方法签名中的final关键字被忽略?如何强制子类在其方法中使用最终变量?
Possible Duplicate:
Final arguments in interface methods - what’s the point?
While trying to experiment a few things, I've ran into a problem that it's described in this page.
interface B {
public int something(final int a);
}
abstract class C {
public int other(final int b);
}
class A extends C implements B {
public int something(int a) {
return a++;
}
public int other(int b) {
return b++
}
}
Why is such feature possible? I don't know why it's possible to to make a final parameter into a non-final one by just overriding the method. Why is the final keyword ignored in a method signature? And how do I obligate sub-classes to use in their methods final variables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Java 按值将参数传递给方法。
因此,对参数的任何更改都无法传播回调用者。由此可见,参数是否被声明为
final
对于调用者来说绝对没有区别。因此,它是方法实现的一部分,而不是其接口的一部分。您想要“强制子类在其方法中使用最终变量”的动机是什么?
Java passes arguments to a method by value.
Therefore, no changes to a parameter can propagate back to the caller. It follows that whether or not the parameter is declared
final
makes absolutely no difference to the caller. As such, it is part of the implementation of the method rather than part of its interface.What's your motivation for wanting to "obligate sub-classes to use in their methods final variables"?
final
对于参数仅意味着该值不得在方法体内更改。这不是方法签名的一部分,并且与子类无关。在接口或抽象方法中拥有最终参数应该是无效的,因为它没有意义。
final
for a parameter only means that the value must not be changed within the method body. This is not a part of the method signature, and is not relevant to subclasses.It should be invalid to have final parameters in interface or abstract methods, because it's meaningless.
最终变量是唯一可以在闭包中使用的变量。因此,如果您想做这样的事情:
这将无法编译,因为编译器要求
val
是最终的。因此将方法签名更改为即可解决问题。局部最终变量也可以:
Final variables are the only ones that can be used in closures. So if you want to do something like this:
This won't compile, as the compiler requires
val
to be final. So changing the method signature towill solve the problem. Local final variable will do just as well:
Java的
final
不是C++的const
; Java 中不存在 const 正确性这样的东西。在 Java 中,人们使用不可变类来实现常量性。事实证明它非常有效,因为与 C++ 不同,人们不能简单地搞乱内存。 (您可以使用 Field.setAccessible(true),然后使用反射。但即使是这种损坏向量也可以通过使用适当配置的安全管理器运行 JVM 来防止。)
Java's
final
is not C++const
; there is no such thing as const-correctness in Java.In Java, one achieves const-ness using immutable classes. It turns out to be quite effective because unlike C++, one cannot simply mess with memory. (You can use
Field.setAccessible(true)
, and then use Reflection. But even that corruption-vector can be prevented by running the JVM with an appropriately configured security manager.)参数的 Final 关键字不是方法签名的一部分,仅对方法体很重要,因为 Java 按值传递所有参数(始终为方法调用创建值的副本)。
如果编译器强制我将其设为最终关键字,我仅使用 Final 关键字(用于参数),因为该参数在方法中定义的匿名类中使用。
The final keyword for arguments is not part of the method signature, and is only important for the body of the method, because Java passes all arguments by value (a copy of the value is always made for the method call).
I only use the final keyword (for arguments) if the compiler forces me to make it final, because the argument is used inside an anonymous class defined in the method.
在Java中,参数是按值传递的。参数是否是最终参数仅影响该方法,而不影响调用者。我不明白为什么一个类需要强制子类型。
In Java parameters are passed by value. Whether a parameter is final or not only affects that method, not the caller. I don't see why a class needs to obligate the subtypes.
请注意,
final
参数有一个主要用途:您无法为其分配新值。另请注意,参数始终按值传递,因此调用者不会看到方法内参数的任何赋值。
如果您确实想强制参数为最终参数(为了防止意外重新分配参数时可能引入的错误),请使用代码分析器,例如 checkstyle。
Note that
final
parameters have one main purpose: you can't assign new values to them.Also note that parameters are always passed by value, thus the caller won't see any assignments to the parameter inside the method.
If you really want to force parameters to be final (in order to prevent bugs that might be introduced when reassigning a parameter accidentially), employ a code anaylzer such as checkstyle.