使用继承的重载方法 [英] Using inherited overloaded methods

查看:143
本文介绍了使用继承的重载方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类:

public class ClassA {
    public void method(Number n) {
        System.out.println("ClassA: " + n + " " + n.getClass());
    }
}

和:

public class ClassB extends ClassA {            
    public void method(Integer d) {
        System.out.println("ClassB: " + d + " " + d.getClass());
    }
}

但是当我跑步时:

ClassA a = new ClassB(); 
a.method(3);

我得到:

ClassA: 3 class java.lang.Integer

我的问题是,为什么不是't ClassB 正在使用的方法? a ClassB 的实例, ClassB 's method()整数参数...

My question is, why isn't ClassB's method being used? a is an instance of ClassB, and ClassB's method() has an Integer parameter...

推荐答案


我的问题是,为什么不使用ClassB的方法?

My question is, why isn't ClassB's method being used?

不正确。使用的方法是 ClassB 的方法,它继承自 ClassA

Not true. The method used is ClassB's method, which it inherited from ClassA.

我认为这里混淆的主要原因是该方法实际上不是重写,而是重载即可。虽然 Integer Number 的子类型,但由于方法参数在Java中是不变的,方法 public void方法(整数d)不会覆盖方法 public void method(Number n)。因此, ClassB 最终会有两个(重载)方法。

I think the main reason behind the confusion here is the fact that the method actually is not overridden, instead it is overloaded. Although Integer is a subtype of Number, since method parameter is invariant in Java, the method public void method(Integer d) doesn't override the method public void method(Number n). So, ClassB ends up having two (overloaded) methods.

静态绑定用于重载方法,以及编译器选择具有最具体参数类型的方法。但在这种情况下,为什么编译器选择 public void方法(Number n)而不是 public void method(Integer d)。这是因为您用于调用方法的引用类型为 ClassA

Static binding is used for overloaded methods, and the method with most specific parameter type is chosen by the compiler. But in this case, why does the compiler pick public void method(Number n) instead of public void method(Integer d). That's because of the fact that the reference that you are using to invoke the method is of type ClassA.

ClassA a = new ClassB(); //instance is of ClassB (runtime info)
a.method(3);             //but the reference of type ClassA (compiletime info)

唯一的方法 ClassA public void方法(Number n),这就是编译器选择的内容。请记住,这里预期的参数类型是 Number ,但传递的实际参数(整数3)是自动装入类型 Integer 。它起作用的原因是因为方法参数在Java中是协变的

The only method that ClassA has is public void method(Number n), so that's what the compiler picks up. Remember, here the expected argument type is Number, but the actual argument, the integer 3, passed is auto-boxed to the type Integer. And the reason that it works is because the method argument is covariant in Java.

现在,我认为很明显为什么打印

Now, I think it's clear why it prints


ClassA:3 class java.lang.Integer

ClassA: 3 class java.lang.Integer

这篇关于使用继承的重载方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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