java Java虚方法调用有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11431185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:06:18  来源:igfitidea点击:

What is the use of Java virtual method invocation?

javaoopvirtual-functions

提问by Meenakshi

I understand what is java method invocation and have practiced many examples using it.

我了解什么是 java 方法调用,并使用它练习了许多示例。

I want to know what is the practical situation or need of this concept. It would be of great help if anyone could give a real world scenario where it is used and what would happen if this concept would have not been there?

我想知道这个概念的实际情况或需要是什么。如果有人能给出一个使用它的真实世界场景,如果没有这个概念会发生什么,那将会有很大帮助?

回答by Keppil

Here is an example. Suppose we have 2 classes:

这是一个例子。假设我们有 2 个类:

class A {
    public String getName() {
        return "A";
    }
}

class B extends A {
    public String getName() {
        return "B";
    }
}

If we now do the following:

如果我们现在执行以下操作:

public static void main(String[] args) {
    A myA = new B();
    System.out.println(myA.getName());
}

we get the result

我们得到结果

B

If Java didn't have virtual method invocation, it would determine at compile time that the getName()to be called is the one that belongs to the Aclass. Since it doesn't, but determines this at runtime depending on the actual class that myApoints to, we get the above result.

如果 Java 没有virtual method invocation,它将在编译时确定getName()要调用的是属于A该类的那个。由于它没有,而是在运行时根据myA指向的实际类来确定这一点,因此我们得到了上述结果。

[EDIT to add (slightly contrived) example]
You could use this feature to write a method that takes any number of Objects as argument and prints them like this:

[编辑添加(稍微做作)示例]
您可以使用此功能编写一个方法,该方法将任意数量的Objects 作为参数并像这样打印它们:

public void printObjects(Object... objects) {
  for (Object o: objects) {
    System.out.println(o.toString());
  }
}

This will work for any mix of Objects. If Java didn't have virtual method invocation, all Objects would be printed using Object′s toString()which isn't very readable. Now instead, the toString()of each actual class will be used, meaning that the printout will usually be much more readable.

这适用于任何对象的组合。如果 Java 没有virtual method invocation,则所有对象都将使用 Object 打印toString(),这不是很可读。现在,toString()将使用每个实际类的 ,这意味着打印输出通常会更具可读性。

回答by Marko Topolnik

OK, I'll try to provide a simple example. You are writing a method that will fill a caller-supplied list:

好的,我将尝试提供一个简单的示例。您正在编写一个方法来填充调用者提供的列表:

public void fill(List l) {
  list.add("I just filled the list!");
}

Now, one caller wants to use a linked list; another one prefers a list implementation based on an array. There will be other callers with even more list implementations that you've never even heard of. These are totally different objects. Propose a solution that achieves this without relying on virtual methods.

现在,一个调用者想要使用链表;另一个更喜欢基于数组的列表实现。还会有其他调用者具有更多您从未听说过的列表实现。这些是完全不同的对象。提出一种解决方案,在不依赖虚拟方法的情况下实现这一点。

Without virtual methods this would mean that the type Listwould already need to have the method addimplemented. Even if you had a subtype ArrayListwhich had an overridden method, the compiler (and the runtime!) would simply ignore that method and use the one in List. It would be impossibleto use different Listimplementations that conform to the same interface; it would be impossible to reuse that line of code in the method fillsince it would work only with the method in the type List.

如果没有虚拟方法,这将意味着该类型List已经需要add实现该方法。即使您的子类型ArrayList有一个被覆盖的方法,编译器(和运行时!)也会简单地忽略该方法并使用List. 这将是不可能使用不同的List符合同一接口的实现; 不可能在方法中重用该行代码,fill因为它只能与 type 中的方法一起使用List

So you see, the whole idea of type hierarchy wouldn't make a lot of sense; interfaces and abstract classes couldn't even exist. The whole of Java would break down into shards without that one feature of virtual methods.

所以你看,类型层次结构的整个想法没有多大意义;interfaces 和abstract classes 甚至不可能存在。如果没有虚拟方法的这一特性,整个 Java 就会分解成碎片。