我正在读一本关于Java的书,它说你可以将整个类声明为final。我想不出有什么地方可以用它。

我只是一个编程新手,我想知道程序员是否真的在他们的程序中使用这个。如果他们使用,他们什么时候使用,这样我就能更好地理解它,知道什么时候使用它。

如果Java是面向对象的,并且你声明了一个final类,难道它不会阻止类具有对象的特征吗?


当前回答

Be careful when you make a class "final". Because if you want to write an unit test for a final class, you cannot subclass this final class in order to use the dependency-breaking technique "Subclass and Override Method" described in Michael C. Feathers' book "Working Effectively with Legacy Code". In this book, Feathers said, "Seriously, it is easy to believe that sealed and final are a wrong-headed mistake, that they should never have been added to programming languages. But the real fault lies with us. When we depend directly on libraries that are out of our control, we are just asking for trouble."

其他回答

其他的答案集中在final类告诉编译器什么:不允许另一个类声明它扩展了这个类,以及为什么这样做是可取的。

但是编译器并不是短语final类的唯一读者。每个读源代码的程序员也会读这个。它可以帮助快速理解程序。

In general, if a programmer sees Thing thing = that.someMethod(...); and the programmer wants to understand the subsequent behaviour of the object accessed through the thing object-reference, the programmer must consider the Thing class hierarchy: potentially many types, scattered over many packages. But if the programmer knows, or reads, final class Thing, they instantly know that they do not need to search for and study so many Java files, because there are no derived classes: they need study only Thing.java and, perhaps, it's base classes.

是的,有时您可能需要这样做,无论是出于安全性还是速度原因。在c++中也可以做到。它可能不适用于程序,但更适用于框架。 http://www.glenmccl.com/perfj_025.htm

首先,我推荐这篇文章:Java:何时创建最终类


如果他们使用,他们什么时候使用,这样我就能更好地理解它,知道什么时候使用它。

final类只是一个不能扩展的类。

(这并不意味着对该类对象的所有引用都将被声明为final。)

何时将一个类声明为final是有用的,这在这个问题的答案中涵盖:

在Java中禁止继承的好理由?

如果Java是面向对象的,并且你声明了一个final类,难道它不会阻止类具有对象的特征吗?

在某种意义上是的。

通过将一个类标记为final,您将禁用该语言的这部分代码的强大而灵活的特性。然而,有些类不应该(在某些情况下也不能)在设计时很好地考虑到子类化。在这些情况下,将类标记为final是有意义的,尽管它限制了OOP。(记住,final类仍然可以扩展另一个非final类。)

final类可以避免在添加新方法时破坏公共API

假设在基类的版本1中:

public class Base {}

客户会这样做:

class Derived extends Base {
    public int method() { return 1; }
}

然后,如果在版本2中,你想添加一个方法method到Base:

class Base {
    public String method() { return null; }
}

这将破坏客户端代码。

如果我们使用final类Base,客户端就不能继承,方法的添加也不会破坏API。

面向对象不是关于继承,而是关于封装。继承会破坏封装。

在很多情况下,声明类final是非常有意义的。任何代表“价值”的对象,如颜色或金额,都可能是最终的。他们是独立的。

如果您正在编写类库,请将类设为final,除非您显式地将它们缩进以派生。否则,人们可能会派生你的类并重写方法,破坏你的假设/不变量。这也可能有安全隐患。

Joshua Bloch在《Effective Java》一书中建议明确地为继承而设计,或者禁止为继承而设计,他指出为继承而设计并不是那么容易。