为什么静态方法无法隐藏java中的实例方法 [英] Why static method cannot hide instance method in java

查看:197
本文介绍了为什么静态方法无法隐藏java中的实例方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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中的实例方法,有人可以解释这一点,提前谢谢。

Test class is super class and Fest is it's sub class as we know static methods can not be overridden even then i am getting error like "static method cannot hide instance method in java" can someone explain this, thanks in advance.

推荐答案

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

The term overriding is generally referred for objects because using same parent reference, you can call different methods from given child object. In this manner, static methods cannot be overridden because they are associated with reference type, not object type.

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

How overriding happens? You specify a method in base class and put same signature in child class, this automatically overrides the method. If you note, static methods are also inherited, i.e., if a parent class contains a static method, then it can be used by child class reference. For example:

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");
    } 
}

现在,静态方法已经到来您的子类,并且您正尝试在具有相同签名的子类中定义新方法。这导致了问题。如果您在一个班级中进行此操作,则错误消息会有所不同。

Now, a static method has come to your child class and you are trying to define a new method in child class with same signature. This is causing the problem. If you are doing it in a single class, error message would be different.

案例1:同一班级

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");
    }
}

这篇关于为什么静态方法无法隐藏java中的实例方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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