Java继承(方法覆盖和重载) [英] Java inheritance (method overriding and overloading)

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

问题描述

除了Java继承是该语言的基本功能之外,我还有一些问题。

以下是我的测试示例的来源:

Besides that Java inheritance is a fundamental feature of the language, I have some questions.
Here is the source for my testing example:

class MyClass{

    public void say(String t){
        System.out.println("Hello MyClass "+t);
    }

    public void print(MyClass t){
        System.out.println("MyClass is printed");
    }

     public void anotherPrint(int i){
        System.out.println("MyClass is printed again");
    }
}

class MyClass2 extends MyClass{

    public void say(String t){
        System.out.println("Hello MyClass2 "+t);
    }

    public void print(MyClass2 t){
        System.out.println("MyClass2 is printed");
    }

    public void anotherPrint(double i){
        System.out.println("MyClass2 is printed again");
    }
}

public class HelloWorld{

     public static void main(String []args){
        MyClass klass = new MyClass2();

        klass.say("h"); //Question 1 (Prints: "Hello MyClass2 h")

        klass.print(new MyClass2()); //Question 2 (Prints: "MyClass is printed")
        klass.print(new MyClass()); //Question 3 (Prints: "MyClass is printed")

        klass.anotherPrint(1); //Question 4 (Prints: "MyClass is printed again")
        klass.anotherPrint(1.0); //Question 5 (Throws Exception!)
     }
}

我有以下问题:


1. klass对象是MyClass的实例。为什么它从MyClass2类执行方法?


2,3。在问题1中,klass调用Class2类的方法。在这里,我使用了一个适合每个重写和重载(同时)方法的参数。为什么klass对象总是从MyClass类调用该方法?


4.这是正常的。毫无疑问。


5.抛出异常是正确的。 klass对象没有带双参数的方法。但是,为什么它不是从MyClass2类调用的方法,就像在问题1中发生的那样?

I have the following questions:

1. The klass object is instance of MyClass. Why does it execute the method from the MyClass2 class?

2,3 . At question 1 klass calls the method of the Class2 class. Here I used a parameter that fits to each one of the overridden and overloaded (simultaneously) methods. Why klass object always calls the method from the MyClass class?

4. It is normal. No question at all.

5. It is right to throw an exception. The klass object does not have this method with double parameter. But, why it is not called the method from the MyClass2 class, just like it happened at Question 1?

推荐答案

1。 klass对象是MyClass的实例。

不是它是 MyClass 类型的引用变量但是引用 MyClass2的对象。

No it is a reference variable of type MyClass but referring to an object of MyClass2.

2。为什么它从MyClass2类执行方法?

因为你要调用说() MyClass2 的对象上,它执行 say() MyClass2 。预期的行为。

Since you are invoking say() on an object of MyClass2, it executes the say() of MyClass2. Expected behavior.

这在Java中称为运行时多态性。这提供了覆盖类层次结构树中已有的功能的能力。在运行时,将调用哪个版本的方法取决于存储在该引用变量中的实际对象的类型,而不是基于引用变量的类型。

This is called the run time polymorphism in Java. This provides the ability to override functionality already available in the class hierarchy tree. At run time, which version of the method will be invoked is based on the type of actual object stored in that reference variable and not on the type of the reference variable.

3。在问题1中,klass调用Class2类的方法。在这里,我使用了一个适合每个重写和重载(同时)方法的参数。为什么klass对象总是从MyClass类调用该方法?

这不是重写方法。子类中的实例方法与相同的签名(名称,加上其参数的数量和类型)和返回类型作为超类中的实例方法会覆盖超类的方法。覆盖方法也可以返回被重写方法返回的类型的子类型。这称为协变返回类型。您的方法签名是不同的。因此调用 klass.print()其中 klass MyClass 引用将始终引用 print() MyClass

That is not overridden method. An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.An overriding method can also return a subtype of the type returned by the overridden method. This is called a covariant return type.Your method signatures are different. Hence invoking klass.print() where klass is a MyClass reference will always refer to the print() of MyClass.

4。抛出异常是正确的。 klass对象没有带双参数的方法。但是,为什么它不是从MyClass2类调用的方法,就像它在问题1中发生的那样?

因为在编译时,编译器会验证是否您可以根据引用类型调用方法。这里的引用类型是 MyClass ,因为 MyClass 没有定义 anotherPrint(double),编译器抱怨。这是一个编译时检查。在问题1中,编译器验证了 klass.say(hi)并且它看到了那里在 MyClass 中存在一个可以这种方式调用的方法。那时,它不关心 klass 引用变量是否会引用 MyClass 对象或运行时的MyClass2 对象。因此它有效。

Because at compile time, the compiler validates if you can call a method based on the reference type. Here the reference type is MyClass and since MyClass doesn't define anotherPrint(double), the compiler complains.It is a compile time check.In question 1, compiler verified klass.say("hi") and it saw that there exists a method in MyClass which can be invoked this way. At that time, it was not concerned whether klass reference variable will refer to a MyClass object or MyClass2 object at runtime. Hence it worked.

您可以参考这些Oracle的教程这里

You can refer to these Oracle's tutorials here.

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

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