java - 如何检查方法覆盖与否

标签 java

我有两个问题 1 覆盖 2 编译时绑定(bind)
嗨,我想知道如何检查 sh() 是否被覆盖

方法参数是否在方法重载中起作用?

为什么我们说静态方法在编译时绑定(bind)但实际上静态方法在类加载时分配内存? 当我使用 javac 工具时,这意味着我使用编译器并编译了一个 java 文件,所以那一刻静态内存不分配,静态内存分配一个类加载时间 那为什么说静态方法使用编译时绑定(bind)

类加载时间和编译时间一样吗? 我很困惑

我知道这里的方法签名是不同的,所以这里没有覆盖而不是这里实际发生的解释

class A
    {
        void sh(char x){
        System.out.println("value of x :  "+x);
        }
    }
    class B extends A
    {
        public void sh(int x)
        {
            System.out.println("value of x"+x);
        }
    }
    class C
    {
        public static void main(String...Aa) /* ??? */
        {
            A a1=new B();
            //a1.show();
            a1.sh('a');
            a1.sh(10);
        }
    } 

最佳答案

Java Language Spec

An instance method m1, declared in class C, overrides another instance method m2, declared in class A iff all of the following are true:

  • C is a subclass of A.

  • The signature of m1 is a subsignature (§8.4.2) of the signature of m2.

  • Either:

    • m2 is public, protected, or declared with default access in the same package as C, or

    • m1 overrides a method m3 (m3 distinct from m1, m3 distinct from m2), such that m3 overrides m2.

  • Moreover, if m1 is not abstract, then m1 is said to implement any and all declarations of abstract methods that it overrides.

subsignature is here 的定义.你问

is method argument play any role in method overriding ?

根据以上所述,是的,非常重要。您的签名必须匹配。也就是说

public void sh(int x)

不覆盖

void sh(char x){

why we say that static method bind at compile time but actually static method allocate memory at the class loading time ?

在编译时,方法调用在引用的静态或声明类型上解析。换句话说,如果类型没有声明这样的方法,程序将无法编译。对于 static 方法。如果该方法是static,则该方法会立即解析并绑定(bind)到调用它的类型。如果它是一个 instance 方法,则绑定(bind)是通过多态性动态解析(后期绑定(bind))的。

这些都与类加载或分配内存无关。

关于java - 如何检查方法覆盖与否,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19058760/

相关文章:

java - 使单个线程执行完成

Java读取二进制文件(unsigned long long)

java - 图像/纹理滚动/移位(换行)

java - Gson避免反射

java - JBoss错误报告: HTTP Status 404 - Servlet is not available

Java:如何编写泛型方法?

java.lang.NoClassDefFoundError : jdk/nashorn/api/scripting/ScriptUtils in osgi bundle

java - maven 依赖 jar 文件显示为文件夹图标

java - Java ThreadFactory 的问题

java - 如何在树中找到选定的节点?