Java`final`方法:它承诺什么? [英] Java `final` method: what does it promise?

查看:25
本文介绍了Java`final`方法:它承诺什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java 类中,可以将方法定义为 final,以标记此方法不能被覆盖:

In a Java class a method can be defined to be final, to mark that this method may not be overridden:

public class Thingy {
    public Thingy() { ... }
    public int operationA() {...}
    /** this method does @return That and is final. */
    public final int getThat() { ...}
}

这很清楚,它可能对防止意外覆盖或性能有所帮助 -mdash;但这不是我的问题.

That's clear, and it may be of some use to protect against accidental overriding, or maybe performance — but that's not my question.

我的问题是:从 OOP 的角度我理解,通过定义一个方法 final 类设计者 承诺 这个方法将始终如描述或暗示的那样工作.但通常这可能不受类作者的影响,如果方法正在做的更复杂,那么只需提供一个属性.

My question is: From an OOP point of view I understood that, by defining a method final the class designer promises this method will always work as described, or implied. But often this may be outside the influence of the class author, if what the method is doing is more complicated then just delivering a property.

句法约束对我来说很清楚,但在 OOP 意义上有什么含义?大多数类作者在这个意义上正确使用 final 吗?

The syntactic constraint is clear to me, but what is the implication in the OOP sense? Is final used correctly in this sense by most class authors?

final 方法承诺什么样的契约"?

What kind of "contract" does a final method promise?

推荐答案

如前所述,final 与 Java 方法一起使用来标记该方法不能被覆盖(对于对象范围)或隐藏(静态).这允许原始开发人员创建子类无法更改的功能,这就是它提供的全部保证.

As mentioned, final is used with a Java method to mark that the method can't be overridden (for object scope) or hidden (for static). This allows the original developer to create functionality that cannot be changed by subclasses, and that is all the guarantee it provides.

这意味着如果该方法依赖于其他可自定义的组件,如非公共字段/方法,则最终方法的功能可能仍然是可自定义的.这很好,因为(通过多态)它允许部分定制.

This means that if the method relies on other customizable components like non-public fields/methods the functionality of the final method may still be customizable. This is good though as (with polymorphism) it allows for partial customization.

阻止某些内容可定制的原因有很多,包括:

There are a number of reasons to prevent something from being customizable, including:

  • 性能 -- 一些编译器可以分析和优化操作,尤其是没有副作用的编译器.

  • Performance -- Some compilers can analyse and optimise the operation, especially the one without side-effects.

获取封装数据 -- 查看不可变对象,它们的属性在构造时设置并且永远不应更改.或者从这些属性派生的计算值.一个很好的例子是 Java String 类.

Obtain encapsulated data -- look at immutable Objects where their attributes are set at the construction time and should never be changed. Or a calculated value derived from those attributes. A good example is the Java String class.

可靠性和契约 -- 对象由原语(intchardouble等)和/或其他对象.并非所有适用于这些组件的操作在更大的对象中使用时都应该适用甚至合乎逻辑.可以使用带有 final 修饰符的方法来确保这一点.Counter 类就是一个很好的例子.

Reliability and Contract -- Objects are composed of primitives (int, char, double, etc.) and/or other Objects. Not all operations applicable to those components should be applicable or even logical when they are used in the bigger Object. Methods with the final modifier can be used to ensure that. The Counter class is a good example.

public class Counter {
    private int counter = 0;

    public final int count() {
        return counter++;
    }

    public final int reset() {
        return (counter = 0);
    }
}

如果 public final int count() 方法不是 final,我们可以这样做:

If the public final int count() method is not final, we can do something like this:

Counter c = new Counter() {   
    public int count() {
        super.count();   
        return super.count();   
    } 
}

c.count(); // now count 2

或类似的东西:

Counter c = new Counter() {
    public int count() {
        int lastCount = 0;
        for (int i = super.count(); --i >= 0; ) {
            lastCount = super.count();
        }

        return lastCount;
    }
}

c.count(); // Now double count

这篇关于Java`final`方法:它承诺什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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