将多态性浪费在申请我们在运行时确切知道类型的类时会浪费吗? [英] Is Polymorphism a waste to apply for the classes that we exactly know the type prior run-time?

查看:97
本文介绍了将多态性浪费在申请我们在运行时确切知道类型的类时会浪费吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行时多态性可用于让运行时动态加载抽象类/接口的确切具体类. (您可以以动物/狗,车辆/汽车为例)

Run-time Polymorphism can be used to let the run-time to dynamically load the exact concrete class of an abstract class/interface. (You can take Animal/Dog, Vehicle/Car examples)

但是,当我们知道确切的具体类@ coding-time(编译时)时,真的需要强制应用多态吗?

But when we know the exact concrete class @coding-time (compile-time), does it really need to forcefully apply polymorphism?

推荐答案

在编写OO代码时,我倾向于使用分配最左侧的通用类型.这立即意味着我对您问题的回答是-不.

When I write OO code, I tend to use most-general type I can on the left-hand side of the assignment. This immediately means that my answer to your question is - no.

这里是例子:

Animal x = new Dog();
...
x.move();

之所以这样做,是因为我可能会将操作的开始和结束分为两个不同的操作.我的方法在实践中非常短.

The reason why I'm doing this is that I'm probably going to split beginning and end of the operation into two distinct operations. My methods are extremely short in practice.

应用于同一示例:

function moveDog() {
    move(new Dog());
}

function move(Animal animal) {
    animal.move();
}

如您所见,移动功能无法知道它真正在移动哪种动物.

As you can see, it would make no sense for the move function to know what kind of animal it is really moving.

通常,编译器有责任确定在给定的代码库中是否已使用重写的move()方法进行了任何具体的调用.一些编译器可以检测到没有重写的方法将受到它们的处理,然后在编译时删除动态分配.幸运的是,无论move函数接收Animal还是Dog,我上面的代码都可以编译相同的代码.

Generally, it is compiler's duty to figure whether in a given code base any concrete call has been made with an overridden move() method. Some compilers can detect that no overridden method will be subjected to them and then they remove dynamic dispatch at compile time. With some luck, my code above would compile the same whether move function receives Animal or Dog.

现在,这是理论.实际上,有两件重要的事情.首先,与需要动态分派的调用相反,被广泛使用的编译器仍未开始使用诸如检测静态方法调用之类的积极优化技术.第二,第一件事与我们今天拥有的CPU功能无关紧要.

Now, this is theory. In practice, there are two important things. First, compilers that are widely used have still not started using such aggressive optimization techniques as detecting static method calls, as opposed to calls that require dynamic dispatch. Second, the first thing doesn't matter too much with CPU power we have today.

我已经在编写高度优化的代码已有15年了,我遇到了不得不将多态调用排除在外的情况.这就是为什么我强烈建议尽可能多地应用多态性的原因.当需要添加一些类以合并新功能时,多态调用很可能是将新类无缝添加到现有设计的工具.如果在开发过程中使用了过于具体的类型,则很容易发生无法将新功能添加到给定代码库的情况.

I have been writing highly optimized code for fifteen years already and I have met the situation in which I had to factor polymorphic calls out. That is why I strongly recommend to apply polymorphism as much as possible. When the time comes to add some classes, to incorporate new features, polymorphic calls will likely be the tool to seamlessly add new classes to the existing design. If you used overly concrete types during development, it could easily happen that you cannot add new feature to the given code base.

这篇关于将多态性浪费在申请我们在运行时确切知道类型的类时会浪费吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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