简体   繁体   English

关于方法调用的 Java 多态性的混淆

[英]Confusion on Java Polymorphism regarding method calling

I am having trouble with this Java Question :我遇到了这个Java 问题的问题

Consider the following classes:考虑以下类:

​
public class Computer extends Mineral {
    public void b() {
        System.out.println("Computer b");
        super.b();
    }

    public void c() {
        System.out.println("Computer c");
    }
}
​
public class Mineral extends Vegetable {
    public void b() {
        System.out.println("Mineral b");
        a();
    }
}
​
public class Animal extends Mineral {
    public void a() {
        System.out.println("Animal a");
    }

    public void c() {
        b();
        System.out.println("Animal c");
    }
}
​
public class Vegetable {
    public void a() {
        System.out.println("Vegetable a");
    }
​
    public void b() {
        System.out.println("Vegetable b");
    }
}

Suppose the following variables are defined:假设定义了以下变量:

Vegetable var1 = new Computer();
Mineral   var2 = new Animal();
Vegetable var3 = new Mineral();
Object    var4 = new Mineral();

Indicate on each line below the output produced by each statement shown.在显示的每个语句产生的输出下方的每一行中指出。 If the statement produces more than one line of output indicate the line breaks with slashes as in a/b/c to indicate three lines of output with a followed by b followed by c.如果语句产生多于一行的输出,则用斜杠表示换行符,如 a/b/c 以表示三行输出,a 后接 b 后接 c。 If the statement causes an error, write the word error to indicate this.如果该语句导致错误,请写出错误一词来表明这一点。

For the execution of为了执行

var1.b()

I was confused about the output我对输出感到困惑

Through careful analysis, we must notice that when we call the method b() of mineral:通过仔细分析,我们必须注意到,当我们调用mineral的b()方法时:

public void b() {
        System.out.println("Mineral b");
        a();
    }

We are also calling a method我们也在调用一个方法

a()

And therefore, using a class hierarachy diagram, can call the method因此,使用类层次结构图,可以调用该方法

Vegetable.a()

source 来源

In Vegetable var1 = new Computer();Vegetable var1 = new Computer(); , you have a reference variable of type Vegetable , pointing to an object of type Computer . ,您有一个Vegetable类型的引用变量,指向一个Computer类型的对象。 The assignment is valid, if Vegetable is a super-type of Computer.如果Vegetable 是Computer 的超类型,则赋值有效。

The expression var1.b() will be legal (compilation pass), if the type of the reference variable (Vegetable) has a method b() .如果引用变量 (Vegetable) 的类型具有方法b() ,则表达式var1.b()将是合法的(编译通过b() If the Vegetable type doesn't have a method b() , then the expression will give a compilation error.如果蔬菜类型没有方法b() ,则表达式将给出编译错误。

If the compilation passes: at runtime , calling var1.b() will invoke the b() method on the object the variable var1 points to (that is, an instance of type Computer).如果编译通过: 在运行时,调用var1.b()将对变量var1指向的对象(即 Computer 类型的实例var1.b()调用b()方法。 Computer.b() overrides Mineral.b() , so that method will be invoked. Computer.b()覆盖Mineral.b() ,因此将调用该方法。

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

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