当我们使用 最终的 说明符,则无法在任何继承类中重写该方法。由于设计原因,方法最终确定。 因为私有方法是不可访问的,所以它们在Java中是隐式的最终方法。所以加上 最终的 私有方法的说明符不会增加任何值。事实上,这可能会造成不必要的混乱。
null
class Base { private final void foo() {} // The above method foo() is same as following. The keyword // final is redundant in above declaration. // private void foo() {} } |
例如,下面的“程序1”和“程序2”都会产生相同的编译器错误“foo()在Base中具有私有访问权限”。
方案1
// file name: Main.java class Base { private final void foo() {} } class Derived extends Base { public void foo() {} } public class Main { public static void main(String args[]) { Base b = new Derived(); b.foo(); } } |
方案2
// file name: Main.java class Base { private void foo() {} } class Derived extends Base { public void foo() {} } public class Main { public static void main(String args[]) { Base b = new Derived(); b.foo(); } } |
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END