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

查看:147
本文介绍了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() { ...}
}

这很清楚,它可能有一些用途,以防止意外超越,或可能性能—但这不是我的问题。

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 类设计器 promises 此方法将始终按描述或隐含的方式工作。但通常这可能超出了班级作者的影响,如果该方法正在做的事情比传递属性更复杂。

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?

什么样的合同呢? 最终方法承诺?

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.

可靠性和合同 - 对象由基元组成( int char double 等)和/或其他对象。并非适用于这些组件的所有操作都应适用,或者在更大的对象中使用时也应该是合乎逻辑的。使用 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天全站免登陆