可以覆盖一个方法,通过向下转换到超类来防止? [英] can overriding of a method be prevented by downcasting to a superclass?

查看:146
本文介绍了可以覆盖一个方法,通过向下转换到超类来防止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解以下问题的答案在所有主要OOP语言中是否相同:



假设我有类 A 定义方法 act jump ;方法 act 调用方法 jump A 的子类 B 覆盖方法 jump 使用适当的语法来确保每当调用 jump 时,使用 B 类中的实现。 p>

我有 B 类的对象 b 。我想让它的行为,就像是 A 。换句话说,我想使用 A 中的实现来执行 jump 。我在不同语言中的选项是什么?



例如,我可以通过某种形式的向下转换来实现这一点吗?或者可以通过创建一个代理对象来知道调用哪些方法。



我想避免创建一个全新的对象 A 并仔细设置 a b 之间的内部状态共享,因为这显然不是future-防伪,复杂。我还想避免将 b 的状态复制到 A 类的全新对象,因为可能存在



我问过这个问题具体关于Python ,<罢工>但似乎这是不可能实现的Python ,从技术上可以做... ... aa ..



看起来除了技术可行性,有一个强烈的论据反对这样做从设计的角度。我在询问一个单独的问题

解决方案

重申的意见: b

当你的子类与他们的超类有明确定义的行为差异时,继承效果很好,但是你经常碰到一个点,在那里模型变得尴尬或停止。此时,您需要重新考虑自己的设计。



组合通常是更好的解决方案。将对象的不同行为委托给不同的对象(或多个对象)可以减少或消除对子类化的需要。



在您的情况下,A类和B类之间的行为差​​异可以封装在策略模式中。然后,您可以在实例级别更改A类(和B类,如果仍然需要)的行为,只需指定一个新策略即可。



策略模式可能需要更多代码在短期内,但它是干净和可维护的。方法swizzling,猴子修补和所有那些酷的东西,让我们在我们的具体语言实现中徘徊是有趣的,但潜在的意想不到的副作用是高的,代码往往难以维护。


I'm trying to understand whether the answer to the following question is the same in all major OOP languages; and if not, then how do those languages differ.

Suppose I have class A that defines methods act and jump; method act calls method jump. A's subclass B overrides method jump (i.e., the appropriate syntax is used to ensure that whenever jump is called, the implementation in class B is used).

I have object b of class B. I want it to behave exactly as if it was of class A. In other words, I want the jump to be performed using the implementation in A. What are my options in different languages?

For example, can I achieve this with some form of downcasting? Or perhaps by creating a proxy object that knows which methods to call?

I would want to avoid creating a brand new object of class A and carefully setting up the sharing of internal state between a and b because that's obviously not future-proof, and complicated. I would also want to avoid copying the state of b into a brand new object of class A because there might be a lot of data to copy.

UPDATE

I asked this question specifically about Python, but it seems this is impossible to achieve in Python and technically it can be done... kinda..

It appears that apart from technical feasibility, there's a strong argument against doing this from a design perspective. I'm asking about that in a separate question.

解决方案

The comments reiterated: Prefer composition over inheritance.

Inheritance works well when your subclasses have well defined behavioural differences from their superclass, but you'll frequently hit a point where that model gets awkward or stops making sense. At that point, you need to reconsider your design.

Composition is usually the better solution. Delegating your object's varying behaviour to a different object (or objects) may reduce or eliminate your need for subclassing.

In your case, the behavioural differences between class A and class B could be encapsulated in the Strategy pattern. You could then change the behaviour of class A (and class B, if still required) at the instance level, simply by assigning a new strategy.

The Strategy pattern may require more code in the short run, but it's clean and maintainable. Method swizzling, monkey patching, and all those cool things that allow us to poke around in our specific language implementation are fun, but the potential for unexpected side effects is high and the code tends to be difficult to maintain.

这篇关于可以覆盖一个方法,通过向下转换到超类来防止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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