【问题标题】:Why static method cannot hide instance method in java为什么静态方法不能在java中隐藏实例方法
【发布时间】:2015-10-21 09:48:12
【问题描述】:
class TestOverriding {

    public static void main(String aga[]) {
        Test t = new Fest();
        t.tests();
    }
}

class Test {
    void tests() {
        System.out.println("Test class : tests");
    }
}
class Fest extends Test {   
    static void tests() {
        System.out.println("Fest class : tests");
    } 
}

测试类是超类,而 Fest 是它的子类,因为我们知道静态方法不能被覆盖,即使这样我也遇到了“静态方法无法在 java 中隐藏实例方法”之类的错误,有人可以解释一下吗,在此先感谢。

【问题讨论】:

    标签: java


    【解决方案1】:

    术语覆盖通常用于对象,因为使用相同的父引用,您可以从给定的子对象调用不同的方法。通过这种方式,静态方法不能被覆盖,因为它们与引用类型相关联,而不是对象类型。

    重写是如何发生的? 你在基类中指定一个方法并在子类中放置相同的签名,这会自动重写该方法。如果你注意到,静态方法也是继承的,即如果父类包含静态方法,那么它可以被子类引用使用。例如:

    public class TestOverriding {
    
        public static void main(String aga[]) {
            Fest t = new Fest();
            t.tests(); <-- prints "Test class : tests"
        }
    }
    
    class Test {
        static void tests() {
            System.out.println("Test class : tests");
        }
    }
    class Fest extends Test {   
        void tests3() {
            System.out.println("Fest class : tests");
        } 
    }
    

    现在,您的子类中出现了一个静态方法,您正尝试在子类中定义一个具有相同签名的新方法。这是导致问题的原因。如果您在单个类中执行此操作,则错误消息会有所不同。

    案例一:同班

    class Fest{   
        void tests3() {
            System.out.println("Fest class : tests3");
        } 
        static void tests3() {
            System.out.println("Fest class : static tests3"); <-- gives "method tests3() is already defined in class Fest"
        }
    }
    

    案例 2:子类(静态到实例)

    class Test {
        static void tests() {
            System.out.println("Test class : tests");
        }
    }
    class Fest extends Test {   
        void tests() { <-- gives "overridden method is static"
            System.out.println("Fest class : tests");
        }
    }
    

    案例 2:子类(实例到静态)

    class Test {
        oid tests() {
            System.out.println("Test class : tests");
        }
    }
    class Fest extends Test {   
        static void tests() { <-- gives "overriding method is static"
            System.out.println("Fest class : tests");
        }
    }
    

    【讨论】:

      【解决方案2】:

      在静态方法中使用与超类中的实例方法相同的方法名称是不好的做法,因为它非常令人困惑。

      我会说编译器很可能会保护您免受意外错误的影响,因为可能假设您确实想要覆盖该方法。

      【讨论】:

        【解决方案3】:

        在这种情况下,Fest t = new Fest(); t.tests() 之类的东西太令人困惑了:它是什么意思?调用继承的实例方法Test.test?还是调用Fest的静态方法?

        【讨论】:

        • 谢谢@Jean-Baptiste Yunès 我现在知道了
        【解决方案4】:
        ClassA{
        
           public void disp(){}
        
        //Error ,We can not define two members with same name (Note this till next step)
          //public static void disp(){}
        
        }
        
        ClassB extends ClassA{
        
        //Now ClassB has one method disp() inherited from ClassA.
        //Now declaring a static method with same 
        //Error, as mentioned ClassB already has disp() and one more member with same //name is not allowed.
          //public static void disp(){}
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-10-06
          • 2011-04-26
          • 2011-05-10
          • 2011-03-02
          • 1970-01-01
          • 1970-01-01
          • 2015-05-31
          • 2011-12-15
          相关资源
          最近更新 更多