将方法定为最终方法的推理
抱歉,这里有一个简单的问题,刚刚在我的笔记中发现了一些我不明白的与使方法最终相关的内容。我的笔记声称你应该将一个方法定为最终方法,因为这个原因:
使得强制不变量变得不可能。
字符串应该表现得像字符串。
我不太明白这是什么意思。有人可以帮我分解一下吗?多谢。
Sorry, quick question here, just found something in my notes I don't understand in relation to making a method final. My notes claim you should make a method final for this reason :
Makes it impossible to enforce invariants.
A String should behave as a String.
I don't really understand what is meant by this. Can someone please break it down for me ? Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我猜应该说“使强制不变量成为可能”。基本上,如果有人可以重写方法,那么他们就可以更改影响类不变量的行为。
I would guess that should have said "Make it possible to enforce invariants". Basically, if someone can override a method, then they can change behaviors affecting your class's invariants.
使方法最终确定的原因通常有两个:性能和设计。当一个方法是最终方法时,它可以被内联。在HotSpot编译之前(JDK 1.1.x),这些方法通常在编译时内联,而在HotSpot中,它们在运行时内联,除非编译器能够保证内联方法始终与使用它的代码一起编译。
我知道将局部变量或参数设置为最终变量有两个原因。第一个原因是您不希望代码更改局部变量或参数。许多人认为更改方法内的参数是不好的风格,因为它会使代码不清楚。作为一种习惯,一些程序员将所有参数设置为“最终”,以防止自己更改它们。我不这样做,因为我发现它使我的方法签名有点难看。
当我们想要从内部类中访问局部变量或参数时,就会出现第二个原因。据我所知,这就是最终局部变量和参数在 JDK 1.1 中被引入 Java 语言的实际原因。
来源
There are typically two reasons to make a method final, performance and design. When a method is final, it may be inlined. Before HotSpot compiling (JDK 1.1.x), these methods were usually inlined at compile time, whereas with HotSpot they are inlined at runtime, unless the compiler can guarantee that the inlined method will always be compiled together with the code that uses it.
There are two reasons I know for making a local variable or a parameter final. The first reason is that you don't want your code changing the local variable or parameter. It is considered by many to be bad style to change a parameter inside a method as it makes the code unclear. As a habit, some programmers make all their parameters "final" to prevent themselves from changing them. I don't do that, since I find it makes my method signature a bit ugly.
The second reason comes in when we want to access a local variable or parameter from within an inner class. This is the actual reason, as far as I know, that final local variables and parameters were introduced into the Java language in JDK 1.1.
Source
是的,基本上将任何东西定为最终意味着你无法改变它。出于同样的原因,人们可以将一个方法定义为类中的最终方法,以使其他子类无法覆盖该方法的功能。这适用于您绝对希望该函数始终执行这一操作的情况。
Yeah basically making anything final means you cannot change it. By this same reasoning, one would make a method final within a class to make it impossible for other subclasses to override this method's functionality. This is applicable to situations where you would absolutely want this one function to always to do this one thing.
如果您的笔记如下,可能会更有意义:
换句话说,完成一个方法可确保它将返回您想要的值,而不是返回其他人错误地认为应该是的覆盖值。
Your notes might make more sense if they read:
In other words, finalizing a method ensures that it will return the value that you mean it to, instead of returning an overridden value that someone else mistakenly thought it should be.
这些注释没有多大意义——我认为不可能应该是“可能”。这个想法是,如果你不希望子类改变方法的行为,你可以将该方法标记为final,并且它将无法被重写。与将整个类标记为最终类相比,这提供了更细粒度的控制。
Those notes don't make much sense - I think impossible should be 'possible'. The idea is that if you don't want subclasses to change the behavior of a method, you can mark the method as final and it will not be able to be overridden. This gives finer grained control than marking the entire class as final.
基本上,如果你将一个方法设为final,那么你就不能在子类中重写该方法。
如果您的应用程序的行为依赖于某种方法,该方法以某种方式完全表现,并且您不希望其他开发人员参与并更改此行为,那么您可以使用“final”关键字。
Basically if you make a method final, then you can't override that method in a subclass.
If the behaviour of your application relies on a certain method behaving exactly in a certain way and you don't want other developers to come along and change this behaviour, then you can use the 'final' keyword.
String
在创建时是不可修改的。这意味着它的所有内部字段都被声明为最终的,并且它不提供更新其内部内容的方法。因此,当您将字符串声明为“i am string”时,您就知道声明的字符串将保留该值直到时间结束。当您不希望某个方法被扩展您的类的类覆盖时,应将该方法声明为final。如果这些方法中的任何一个更改了类内的变量,则仅具有最终方法的类仍然可以是可变的。
我喜欢使用不可修改的类,它确实可以帮助我随时了解程序的状态,并以某种方式防止难以捕获的错误。
A
String
is unmodifiable upon its creation. What that means is that all its internal fields are declared final, and it provide no method to update its internal contents. Therefore, when you declare a string as "i am string", you know that the string you declared will hold that value until the end of the time.A method should be declared final when you don't want the method to be overridden by a class that extends yours. A class that has only final methods can still be mutable if any of these methods change the variables inside the class.
I like using unmodifiable classes, it does help me to know the state of my program at any time and in a way it prevent bugs that would be hard to catch.
打印“Not A: A”
如果超类方法“print(”是final,这种类型的错误就不会发生。
通过final,您可以强制此方法保持不变,以便A始终可以高兴地打印“A”
Prints "Not A: A"
If the super class method "print(" were final this type of bug could not happen.
Via final you can enforce this method remaining unchanged so that A can always be happy printing "A"