Java中具有不同参数数据类型的继承和重载方法 [英] Inheritance and Overloading methods with different argument data types in Java

查看:32
本文介绍了Java中具有不同参数数据类型的继承和重载方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我分析与重载和继承相关的简单 java 代码时,我希望收到一个与参数数据类型匹配的重载输出.但它不能那样工作.

When I was analyzing a simple java code related with overloading and inheritance I expected to recieve an output that overloads matching the argument's data types. But it doesn't work that way.

代码:

class A {
        public int calc (double num){
            System.out.println("calc A");
            return (int)(num+1);}
    }
class B extends A{
        public int calc (long num){
            System.out.println("calc B");
            return (int)(num+2);}
    }
class C extends B{
        public int calc (int num){
            System.out.println("calc C");
            return num+3;}
    }
class D extends C{
        public int calc (float num){
            System.out.println("calc D");
            return (int)(num+4);}
    }

class Program{
        public static void main(String[] args){
            int num1=10;
            long num2 = num1;

            Object o1 = num1;
            System.out.println("num1 Type: "+o1.getClass().getName());

            Object o2 = num2;
            System.out.println("num2 Type: "+o2.getClass().getName());

            A a1=new D();
            A a2=new D();

            System.out.println("a1 Type: "+a1.getClass().getName());
            System.out.println("a2 Type: "+a2.getClass().getName());

            int result = a1.calc(num1)+a2.calc(num2);
            System.out.println("Number: "+result);
        }
    }

输出:

num1 Type: java.lang.Integer
num2 Type: java.lang.Long
a1 Type: D
a2 Type: D
calc A
calc A
Number: 22

我在这里测试代码:ideone

推荐答案

您的主要问题似乎是关于为什么类型输出与正式类型不匹配.这完全是有意为之,这也是面向对象编程如此强大的原因.

Your main question seems to be about why the type outputs don't match with the formal types. This is entirely intentional, and it's what makes object oriented programming so powerful.

在实例上调用方法时,运行时系统会查看实例的实际类型,并根据其实际类型而不是其形式类型查找要调用的方法.

When a method is invoked on an instance, the runtime system looks at the actual type of the instance, and looks up the method to call based on its actual type, rather than on its formal type.

如果不是这样,您将无法完成任何有用的工作.您希望能够声明一个抽象类 A,其中包含以不同方式实现细节的具体类 BC.但是您也希望能够声明 A 类型的变量,而不用关心它们来自哪里,以及它们实际上是 B 类型还是 类型>C.然后你可以调用作为 A 契约一部分的方法,它会做正确的事情:真正是 B 的东西会调用 B 的实现,对于 C 也是如此.

If this weren't the case, you wouldn't be able to get anything useful done. You want to be able to declare an abstract class A, with concrete classes B and C hanging off it that implement the details in different ways. But you also want to be able to declare variables of type A, without caring where they've come from, and whether they're actually of type B or type C. You can then invoke methods that are part of the contract of A, and it'll do the right thing: something that's really a B will invoke B's implementation, and likewise for C.

至于为什么你最终调用了 Acalc 方法而不是 D 的方法,这又是因为多态的方式作品.变量的形式类型是A;所以当你调用 .calc() 时,类型系统会:

As for why you end up invoking A's calc method rather than D's, this is again because of the way polymorphism works. The formal type of the variables is A; so when you invoke .calc(), the type system will:

  1. A类中找到最合适的方法来匹配编译时的调用
  2. 查看是否在A和实际类型运行时之间被覆盖;
  3. 如果有,则调用被覆盖的版本,如果没有,则调用 A 的版本.
  1. find the most appropriate method in class A to match the call at compile time;
  2. see whether that has been overridden between A and the actual type at runtime;
  3. call the overridden version if there is one, or A's version if not.

但是您根本没有覆盖 calc() 方法:您提供了具有不同签名的方法.所以在第 1 步(编译时),类型系统找到 A.calc(double);在第 2 步(在运行时),它发现这在类层次结构中没有被覆盖;因此,在第 3 步(运行时)中,它会调用 A 的版本.

But you haven't overridden the calc() method at all: you've supplied methods with different signatures. So in step 1 (at compile time) the type system finds A.calc(double); in step 2 (at runtime) it discovers that this hasn't been overridden further down the class hierarchy; in step 3 (runtime) it therefore invokes A's version.

重载在编译时根据形式类型解决;覆盖在运行时根据实际类型解析.

Overloads are resolved at compile time based on formal types; overrides are resolved at runtime based on actual types.

这篇关于Java中具有不同参数数据类型的继承和重载方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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