简体   繁体   English

如何检查方法是否覆盖

[英]how to check a method override or not

i have two question 1 overriding 2 compile time binding 我有两个问题1覆盖2编译时绑定
hi i want to know that how i can check that sh() become override 嗨,我想知道我如何检查sh()成为替代

is method argument play any role in method overriding ? 方法参数在方法重写中起什么作用吗?

why we say that static method bind at compile time but actually static method allocate memory at the class loading time ? 为什么我们说静态方法在编译时绑定,而实际上静态方法在类加载时分配内存? when i use javac tool that means i use compiler and i compile a java file ,so that moment static memory not allocate ,static memory allocate a class loading time then why say that static method use compile time binding 当我使用javac工具时,这意味着我使用编译器并编译了一个java文件,因此当静态内存未分配时,静态内存分配了类加载时间,那么为什么要说静态方法使用编译时绑定

is class loading time same as compile time ? 类的加载时间与编译时间相同吗? i am confuse 我感到困惑

i know here method signature is different so no override here than what actually happens here explain 我知道这里的方法签名是不同的,所以这里没有覆盖比实际发生的解释

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

The Java Language Spec states Java语言规范说明

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中声明的实例方法m1覆盖在类A中声明的另一个实例方法m2:

  • C is a subclass of A. C是A的子类。

  • The signature of m1 is a subsignature (§8.4.2) of the signature of m2. m1的签名是m2的签名的子签名(第8.4.2节)。

  • Either: 要么:

    • m2 is public, protected, or declared with default access in the same package as C, or m2是公共的,受保护的或具有默认访问权限的声明,并且与C处于同一包中,或者

    • m1 overrides a method m3 (m3 distinct from m1, m3 distinct from m2), such that m3 overrides m2. m1覆盖方法m3(不同于m1的m3,不同于m2的m3),因此m3覆盖m2。

  • Moreover, if m1 is not abstract, then m1 is said to implement any and all declarations of abstract methods that it overrides. 而且,如果m1不是抽象的,则称m1实现了它所覆盖的抽象方法的所有声明。

The definition for subsignature is here . subsignature的定义在这里 You ask 你问

is method argument play any role in method overriding ? 方法参数在方法重写中起什么作用吗?

According to the above, yes very much so. 根据以上所述,是的。 You signatures have to match. 您的签名必须匹配。 In other words 换一种说法

public void sh(int x)

is not overriding 不覆盖

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 ? 为什么我们说静态方法在编译时绑定,而实际上静态方法在类加载时分配内存?

At compile time, a method call is resolved on the static or declared type of the reference. 在编译时,将对引用的静态或声明的类型解析方法调用。 In other words, the program won't compile if the type doesn't declare such a method. 换句话说,如果类型未声明这种方法,则程序将无法编译。 For static methods. 对于static方法。 If the method is static , then the method is immediately resolved and bound to the type it is called on. 如果该方法是static ,则该方法将立即解析并绑定到其调用的类型。 If it is an instance method, binding is resolve dynamically (late-binding) with polymorphism. 如果是instance方法,则绑定将通过多态性动态解析(后期绑定)。

None of this has anything to do with class loading or allocating memory. 这些都与类加载或分配内存无关。

I'm not clear on what you're asking. 我不清楚你在问什么。 However, when B extends A , B will also inherit the sh(char x) method. 但是,当B扩展AB也将继承sh(char x)方法。 The sh(int x) method does not override this, since the argument type is different. sh(int x)方法不会覆盖此方法,因为参数类型不同。 So an object of class B will have two different methods named sh . 因此, B类的对象将具有两种不同的名为sh方法。

In your code, though, you declared a1 to be of type A . 但是,在您的代码中,您声明a1A类型。 Even though it will (at run time) refer to an object of type B , as far as the compiler knows it is still type A . 即使它(在运行时)引用类型B的对象,据编译器知道它仍然是类型A Therefore, the methods you can apply to this object are the ones declared in A (and its superclasses , if it had any, but it doesn't, other than Object ). 因此,可以应用于此对象的方法是在A声明A (及其超类 (如果有的话,但不是,除了Object ))。 The only method you have (besides the Object methods) is sh(char x) . 您拥有的唯一方法(除Object方法之外)是sh(char x)

So when you say 所以当你说

a1.sh('a');
a1.sh(10);

the compiler will treat this as if the argument is a char , since the only method it will look at is the one that takes a char argument. 编译器会将其视为参数是char ,因为它将查看的唯一方法是采用char参数的方法。 This means that a1.sh(10) will call the sh in A with "character 10" as an argument--EDIT: no it won't; 这意味着a1.sh(10)将以“字符10”作为参数调用Ash –编辑:不,不会; I tried it, and the compiler won't let me convert 10 to a char automatically. 我试了一下,编译器不允许我自动将10转换为char

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM