我正在读一本关于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,这样这些方法函数就不会被改变。如果你想要这样的final方法在一个单独的类中,那么将这个类定义为final类。所以Final类将拥有所有Final方法,而Final方法可以在非Final类中声明和定义。

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


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

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

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

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

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

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

在某种意义上是的。

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

假设你有一个Employee类,它有一个方法greet。当greet方法被调用时,它只是简单地打印Hello everyone!这就是greet方法的预期行为

public class Employee {

    void greet() {
        System.out.println("Hello everyone!");
    }
}

现在,让GrumpyEmployee继承Employee并重写greet方法,如下所示。

public class GrumpyEmployee extends Employee {

    @Override
    void greet() {
        System.out.println("Get lost!");
    }
}

现在在下面的代码中看看sayHello方法。它以Employee实例作为参数,并调用greet方法,希望它会说Hello everyone!但我们得到的是滚开!这种行为变化是因为员工grumpyEmployee = new grumpyEmployee ();

public class TestFinal {
    static Employee grumpyEmployee = new GrumpyEmployee();

    public static void main(String[] args) {
        TestFinal testFinal = new TestFinal();
        testFinal.sayHello(grumpyEmployee);
    }

    private void sayHello(Employee employee) {
        employee.greet(); //Here you would expect a warm greeting, but what you get is "Get lost!"
    }
}

如果Employee类是final类,则可以避免这种情况。想象一下,如果没有将String Class声明为final,厚脸皮的程序员会造成多大的混乱。

其他的答案集中在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.

最好的例子是

公共最终类

这是一个不可变的类,不能扩展。 当然,不仅仅是使类final为不可变。