Java方法关键字“final”及其用途 [英] Java method keyword "final" and its use

查看:167
本文介绍了Java方法关键字“final”及其用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建复杂类型层次结构(多个级别,每个级别有几种类型)时,我喜欢在实现某些接口声明的方法上使用 final 关键字。例如:

  interface Garble {
int zork();
}

接口Gnarf扩展Garble {
/ **
*这与调用{@link #zblah(0)}
*相同/
int zblah();
int zblah(int defaultZblah);
}

然后

 抽象类AbstractGarble实现Garble {
@Override
public final int zork(){...}
}

抽象类AbstractGnarf扩展AbstractGarble实现Gnarf {
//这里我绝对想要修复zblah的默认行为
//不应该允许Gnarf将1设置为默认值,例如
@Override
public final int zblah(){
return zblah(0);
}

//此方法未在此处实现,但在子类
@Override
public abstract int zblah(int defaultZblah);
}

我这样做有几个原因:


  1. 它帮助我开发类型层次结构。当我向层次结构中添加一个类时,非常清楚,我必须实现哪些方法,以及我可能不会覆盖哪些方法(如果我忘记了层次结构的详细信息)

  2. 根据设计原则和模式,我认为重写具体的东西很糟糕,例如模板方法模式。我不希望其他开发者或我的用户这样做。

所以最终关键字对我来说非常适合。我的问题是:



为什么它在野外很少使用?你能告诉我一些例子/原因 final (在类似情况下我的)会非常糟糕吗?

解决方案


为什么它在野外很少使用?


这与我的经历不符。我看到它在各种库中经常使用。只有一个(随机)示例:查看以下抽象类:



http://code.google.com/p/guava-libraries/



,例如com.google.common.collect.AbstractIterator。 peek() hasNext() next() endOfData()是最终的,只留下 computeNext()给实现者。这是一个非常常见的IMO示例。



反对使用 final 的主要原因是允许实现者更改算法 - 你提到了模板方法模式:修改模板方法仍然有意义,或者通过一些前/后动作来增强它(不要用几十个前/后挂钩来欺骗整个类)。 / p>

使用 final 的主要原因是为了避免意外的实现错误,或者当方法依赖于类的内部时未指定的(因此将来可能会改变)。


When I create complex type hierarchies (several levels, several types per level), I like to use the final keyword on methods implementing some interface declaration. An example:

interface Garble {
  int zork();
}

interface Gnarf extends Garble {
  /**
   * This is the same as calling {@link #zblah(0)}
   */
  int zblah();
  int zblah(int defaultZblah);
}

And then

abstract class AbstractGarble implements Garble {
  @Override
  public final int zork() { ... }
}

abstract class AbstractGnarf extends AbstractGarble implements Gnarf {
  // Here I absolutely want to fix the default behaviour of zblah
  // No Gnarf shouldn't be allowed to set 1 as the default, for instance
  @Override
  public final int zblah() { 
    return zblah(0);
  }

  // This method is not implemented here, but in a subclass
  @Override
  public abstract int zblah(int defaultZblah);
}

I do this for several reasons:

  1. It helps me develop the type hierarchy. When I add a class to the hierarchy, it is very clear, what methods I have to implement, and what methods I may not override (in case I forgot the details about the hierarchy)
  2. I think overriding concrete stuff is bad according to design principles and patterns, such as the template method pattern. I don't want other developers or my users do it.

So the final keyword works perfectly for me. My question is:

Why is it used so rarely in the wild? Can you show me some examples / reasons where final (in a similar case to mine) would be very bad?

解决方案

Why is it used so rarely in the wild?

That doesn't match my experience. I see it used very frequently in all kinds of libraries. Just one (random) example: Look at the abstract classes in:

http://code.google.com/p/guava-libraries/

, e.g. com.google.common.collect.AbstractIterator. peek(), hasNext(), next() and endOfData() are final, leaving just computeNext() to the implementor. This is a very common example IMO.

The main reason against using final is to allow implementors to change an algorithm - you mentioned the "template method" pattern: It can still make sense to modify a template method, or to enhance it with some pre-/post actions (without spamming the entire class with dozens of pre-/post-hooks).

The main reason pro using final is to avoid accidental implementation mistakes, or when the method relies on internals of the class which aren't specified (and thus may change in the future).

这篇关于Java方法关键字“final”及其用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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