final方法

    xiaoxiao2021-12-15  29

    class WithFinals { private final void f() { System.out.println("WithFinals.f()"); } // 类中所有的private方法都隐式指定是final private void g() { System.out.println("WithFinals.g()"); } } class OverridingPrivate extends WithFinals { private final void f() { System.out.println("OverridingPrivate.f()"); } private final void g() { System.out.println("OverridingPrivate.g()"); } } class OverridingPrivate2 extends OverridingPrivate { public final void f() { System.out.println("OverridingPrivate2.f()"); } public final void g() { System.out.println("OverridingPrivate2.g()"); } } public class FinalOverridingIllusion { public static void main(String[] args) { OverridingPrivate2 op2 = new OverridingPrivate2(); op2.f(); op2.g(); OverridingPrivate op = op2; 覆盖只有在某方法是基类的接口的一部分时才会出现(必须能将一个对象向上转型为它的基本类型并调用相同的方法)  // op.f(); // op.g(); } } final就是防止继承类修改它的含义
    转载请注明原文地址: https://ju.6miu.com/read-1000260.html

    最新回复(0)