方法签名中的最终关键字 [英] Final keyword in method signatures

查看:124
本文介绍了方法签名中的最终关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

接口方法中的最终参数 - 重点是什么?

在尝试尝试一些事情时,我遇到了一个问题,它在此页

While trying to experiment a few things, I've ran into a problem that it's described in this page.

interface B {
    public int something(final int a);
}

abstract class C {
    public int other(final int b);
}

class A extends C implements B {

    public int something(int a) {
        return a++;
    }

    public int other(int b) {
        return b++
    }
}

为什么这样的功能可能?我不知道为什么可以通过覆盖方法将最终参数变成非最终参数。为什么在方法签名中忽略final关键字?如何强制子类在其方法中使用最终变量?

Why is such feature possible? I don't know why it's possible to to make a final parameter into a non-final one by just overriding the method. Why is the final keyword ignored in a method signature? And how do I obligate sub-classes to use in their methods final variables?

推荐答案

Java将参数传递给方法按值。

因此,参数的任何更改都不会传播回调用者。因此,无论参数是否被声明为 final ,都与调用者完全没有区别。因此,它是该方法的实现的一部分,而不是其接口的一部分。

Therefore, no changes to a parameter can propagate back to the caller. It follows that whether or not the parameter is declared final makes absolutely no difference to the caller. As such, it is part of the implementation of the method rather than part of its interface.

你的是什么想要强制子类在其方法中使用最终变量的动机?

What's your motivation for wanting to "obligate sub-classes to use in their methods final variables"?

这篇关于方法签名中的最终关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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