为什么最终关键字对于不可变类是必要的? [英] Why final keyword is necessary for immutable class?

查看:112
本文介绍了为什么最终关键字对于不可变类是必要的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能否澄清为什么在课堂上我们将最终关键字作为不可变的关键字时需要 final 关键字。
我的意思是,如果我们将所有属性声明为私有和最终属性,那么它也是一个不可变类,不是吗?

Could you please clarify that why final keyword is required before class when we are making it an immutable one. I mean, if we declare all of it's attributes as private and final, then also it is an immutable class, isn't it?

对不起,如果问题似乎很容易,但我真的很困惑。帮助我。

Sorry if the question seems easy, but i am truly confused about it. Help me out.

编辑:
我知道一个声明为final的类不能被子类化..但是如果每个属性都是私有的,那么最终会有什么区别是这样吗?

Editted: I know that a class declared final can't be subclassed.. But if each attribute is private and final then what difference does that make?

推荐答案

正如堆叠器所说, final 确保class不是子类。这一点非常重要,因此任何依赖的代码都可以安全地执行。

As stacker says, final makes sure the class isn't subclassed. That's important so that any code which is relying on its immutability can do so safely.

例如,不可变类型(每个字段也是不可变类型)可以在线程之间自由使用而不用担心数据争用等。现在考虑:

For example, immutable types (where each field is also of an immutable type) can be freely used between threads without worrying about data races etc. Now consider:

public class Person {
    private final String name;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

看起来像你一样可以在线程之间自由共享 Person 实例,没有任何问题。但是当你共享的对象实际一个可变的子类时呢?

That looks like you can share Person instances freely across threads with no problem. But what about when the object you're sharing is actually a mutable subclass:

public class Employee extends Person {
    private String company;

    public Employee(String name, String company) {
        super(name);
        this.company = company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getCompany() {
        return company; 
    }
}

现在 Employee的实例 不是在线程之间共享是安全的,因为它们不是不可变的。但是,执行共享的代码可能只知道它们是 Person 的实例......导致它们产生虚假的安全感。

Now instances of Employee aren't safe to share between threads, because they're not immutable. But the code doing the sharing may only know about them as instances of Person... leading them into a false sense of security.

缓存也是如此 - 缓存和重用不可变类型应该是安全的,对吧?好吧, 可以安全地缓存真正属于不可变类型的实例 - 但是如果你正在处理一个本身不允许变异的类型,但确实允许子类,它突然变得不安全了。

The same goes for caching - it should be safe to cache and reuse immutable types, right? Well, it is safe to cache instances which are genuinely of an immutable type - but if you're dealing with a type which itself doesn't allow mutation, but does allow subclasses, it's suddenly not safe any more.

想想 java.lang.Object 。它没有任何可变字段,但将每个 Object 引用视为对不可变类型的引用显然是个坏主意。基本上,这取决于您是否将不可变性视为类型或对象的属性。一个真正的不可变类型声明任何时候你看到这种类型的引用,你可以将它视为不可变 - 而允许任意子类化的类型不能提出这种说法。

Think about java.lang.Object. It doesn't have any mutable fields, but it's clearly a bad idea to treat every Object reference as if it's a reference to an immutable type. Basically it depends on whether you think about immutability as a property of the type or of objects. A truly immutable type declares "any time you see a reference of this type, you can treat it as immutable" - whereas a type which allows arbitrary subclassing can't make that claim.

顺便说一下,有一个中途的房子:如果你可以将子类限制到只有可信的位置,你可以确保一切都是不可变的,但仍然允许子类化。 Java中的访问使得这很棘手,但是在C#中你可以拥有一个只允许在同一个程序集中进行子类化的公共类 - 提供一个在不变性方面很好而且强大的公共API,同时仍然允许多态性的好处。

As an aside, there's a half-way house: if you can limit the subclassing to only "trusted" places, you can ensure that everything's immutable, but still allow that subclassing. The access in Java makes that tricky, but in C# for example you could have a public class which only allowed subclassing within the same assembly - giving a public API which is nice and strong in terms of immutability, while still allowing for the benefits of polymorphism.

这篇关于为什么最终关键字对于不可变类是必要的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆