试图根据具体情况抑制覆盖是错误的吗? [英] Is it wrong trying to suppress overriding on a case by case basis?

查看:23
本文介绍了试图根据具体情况抑制覆盖是错误的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白为什么通过方法覆盖实现的多态性非常有用.我想问的是,在某些情况下,当多态对象作为参数被接收时(而不是在定义它的类时!),在某些情况下试图抑制它可能会出现什么问题(如果有的话).

I understand why polymorphism achieved through method overriding is very useful. I am asking what problems, if any, might arise with trying to suppress it in certain situations, at the time the polymorphic object is received as an argument (not at the time its class is defined!).

类 Car 描述了汽车的行为.FlyingCar 类描述了可以变形和飞行的汽车的行为.

class Car describes the behavior of a car. class FlyingCar describes the behavior of a car that can transform and fly.

我从某个地方收到了 Car 类或其子类的对象.我无法控制他们传递给我的东西.

I received from somewhere the object of class Car or its subclass. I have no control over what they pass me.

我知道由于我的图形引擎的技术限制,我无法显示飞行汽车.或者我希望玩家在不使用飞行能力的情况下完成特定任务.因此,我想通过让它看起来好像是 Car 类的对象来简单地禁用汽车的飞行能力.我在考虑使用向下转换,但它似乎不起作用.

I know that due to the technical limitations of my graphics engine I cannot display the a flying car. Or perhaps I want the player to finish the particular mission without using the flying capability. Thus, I was thinking to simply disable the car's ability to fly by making it look as if it's an object of class Car. I was thinking of using downcasting, but it appears it won't work.

这也许是不可能的,但如果我找到一种方法用我使用的语言来做到这一点,这是不是设计不好?如果是,为什么?有什么替代方案?

It maybe impossible, but if I find a way to do that in the language that I use, is it bad design? If so, why, and what's the alternative?

我无法使用复制构造函数之类的东西从我收到的类中创建 Car 类的对象,因为由此产生的所有数据的复制成本太高(Car 对象很大).

I can't use something like a copy constructor to create an object of class Car from the one I received because the resulting copying of all the data is too expensive (the Car object is huge).

谢谢!

我想避免在此问题中选择特定语言.一旦我选择了一种语言,答案很可能是技术上不可能",或者这是可能的,但所需的 hack 太危险了"等.

I want to avoid choosing a specific language in this question. Once I pick a language, the answer may well be "it's technically impossible", or "it's possible, but the required hack is too dangerous", etc.

我想了解这是否是糟糕的设计,原因与某种语言的(不)支持它的能力无关.

I want to understand whether this is bad design for reasons unrelated to the (in)ability of a certain language to support it.

推荐答案

我的意见通常是否定的.

My opinion would be generally no.

原因是,即使你能以某种方式让你的 FlyingCar 从现在开始表现得就像它是一辆汽车一样,它仍然已经被操作,就好像它是一辆 FlyingCar,并且可能不再处于 Car 的有效状态.

The reason is that it even if you could somehow make your FlyingCar behave only like it would if it were a Car from this point forward, it's still already been operated on as if it were a FlyingCar, and may no longer be in a valid state for a Car.

也许您的图形引擎无法显示 FlyingCar 的原因是它使用的纹理.但是有人已经在其上调用了 load_property_textures 方法,该方法已将其纹理数据存储在其中.如果你再次调用 load_property_textures ,将 FlyingCar 更改为 Car 会改变发生的情况,但 FlyingCar 不会覆盖 render_car 方法,它只是将数据放在 render_car 会找到它.因此,您组织中的其他一些糟糕的程序员最终会试图调试为什么汽车无法渲染并显示一些有关 FlyingCar 纹理的错误消息.

Maybe the reason your graphics engine can't display a FlyingCar is because of the textures it uses. But someone's already called a load_appropriate_textures method on it, which has stored its texture data inside it. Changing the FlyingCar into a Car would change what happened if you called load_appropriate_textures again, but FlyingCar doesn't override the render_car method, it just puts data where render_car will find it. So some other poor programmer in your organisation will just end up trying to debug why a Car is failing to render with some error message about FlyingCar textures.

也许在这种特殊情况下不会发生这种情况.但它可以.并且有人可以稍后以引入此类问题的方式修改 Car 和 FlyingCar.

Maybe that won't happen in this one particular case. But it could. And someone could modify Car and FlyingCar later in a way that introduces this sort of problem.

一般来说,对于 FlyingCar 来说,好像"它是一辆汽车,你真的必须再次重复所有的初始化(和后续的修改).重复后面的修改通常是不可能的(因为它们没有被记录),重复初始化无非是构造一辆新车.

In general, to a FlyingCar "as if" it were a Car, you really have to repeat all the initialisation (and subsequent modifications) again. Repeating later modifications is generally not possible (because they're not recorded), and repeating the initialisation means nothing more than constructing a new Car.

因此,总的来说"似乎是个坏主意.在任何特定情况下,如果你能找到一种方法来做到这一点,也许你会决定它是可以接受的.程序员每天都在做出妥协,它发生了.但是,如果不可能完全通用地做到这一点,那么您总是会面临这样的风险,即稍后会对 Car 和/或 FlyingCar 进行完全合理的更改,从而使您的黑客不再起作用.

So it seems like "in general" it's a bad idea. In any particular case, if you can find a way to do it, maybe you'll decide it's acceptable. Programmers make compromises every day, it happens. But if it's not possible to do this with full generality, then you always run the risk that later perfectly reasonable changes will be made to Car and/or FlyingCar that make your hacks no longer work.

确实,听起来 FlyingCar 需要具有禁用其飞行功能的功能.事后总是很难坚持这样的事情.

Really, it sounds like FlyingCar needs to have the functionality to disable its flying functionality. Something like that is always really hard to bolt on after the fact.

这篇关于试图根据具体情况抑制覆盖是错误的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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