继承方法需要多一个参数
我有一个有几个孩子的家长班。其中一个子级有一个被重写的方法,由于其特定的内部用法,该方法还需要一个参数。我不想更改方法的签名,因为其他子级中的重写方法不需要此参数,而且,我不想添加类属性,因为在这种情况下它是没有意义的。
你如何处理这些情况?
到目前为止,我已经在方法中添加了一个 NotImplementedException 并创建了一个新的异常,但这确实是我在等待答案时所做的事情,我不想这样做。
在乔恩·斯基特回答后进行编辑
我会尝试弄清楚我是否理解了乔恩的建议。这很有趣。
public abstract class Parent {
public abstract void aMethod(Object parameter);
}
public class NotReallyParentChild {
public Parent createInstance(){
return new Child();
}
}
public abstract class Child extends Parent {
}
嗯,不,我在这里完全错了,我不明白你帖子的第二部分,你能解释一下吗?
I have a parent class with several children. One of the children, has one overridden method that, for its particular internal usage, needs one more parameter. I don't want to change the method's signature because the overridden method in the other children does not need this parameter, also, I don't want to add a class property because it is pointless in this case.
How do you deal with these situations?
So far I've added a NotImplementedException in the method and created a new one, but it really is something I've done while I wait for an answer, I don't want to do it this way.
Edit after Jon Skeet's answer
I'll try to figure out if I've understood what Jon suggested. This is quite interesting.
public abstract class Parent {
public abstract void aMethod(Object parameter);
}
public class NotReallyParentChild {
public Parent createInstance(){
return new Child();
}
}
public abstract class Child extends Parent {
}
Mmmmh no I'm completely wrong here, I don't understand the second part of your post, could you please shed some light on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来你在那里并没有真正的继承关系 - 它打破了 里氏可替换性原则 ,因为如果没有额外的知识,您就不能像使用父级的实例一样使用这个“子级”。
如何使“子”类不从“父”类派生,而是在子类上提供一个方法,该方法将创建父类的某个子类的实例,该实例“知道”适当的额外位信息?如果您可以向我们提供有关类正在执行的操作的更多信息,我们可以提供更具体的示例来说明代码的外观。
编辑:在不了解更多关于您正在做什么的情况下,很难尝试使这一点变得更清楚,但是类似:
ChildWithParameter
类可以覆盖适当的方法而无需获取任何额外的信息,因为它已经将其作为其内部状态的一部分。It sounds like you don't really have a proper inheritance relationship there - it breaks Liskov's Subtitutability Principle, because you can't use this "child" as if it were an instance of the parent, without extra knowledge.
How about making the "child" class not derive from the "parent", but provide a method on the child which will create an instance of some subclass of the parent, which "knows" the appropriate extra bit of information? If you could give us more information about what the classes are doing, we could give more concrete examples of what the code might look like.
EDIT: It's really hard to try to make this much clearer without knowing more about what you're doing, but something like:
That
ChildWithParameter
class could then override the appropriate method without taking any extra information, because it already had it as part of its own internal state.虽然同意 Jon 的观点,即继承模型已被破坏,但您可以通过使用特殊参数对象来处理此类场景。也就是说,不是有一个方法:
您有一个包含所需参数的对象
ParameterObject
,并且您的方法如下所示:这样,您可以向参数对象添加参数,并且不会影响或更改接口和实现的方法。
如果我正在编写一个方法并且参数数量似乎过多,并且我相信我的封装不会以任何方式被破坏(当方程/模型需要大量输入时,这种情况在金融软件中经常发生),然后我发现上面是一个非常有用的模式。
Whilst agreeing with Jon's view that the inheritance model is broken, you can handle scenarios like this by using special parameter objects. That is, instead of having a method:
you have an object
ParameterObject
which contains the parameters required, and your method looks like:That way, you can add parameters to the parameter object, and not affect or change the interface and implemented methods.
If I'm writing a method and the number of parameters seems excessive, and I believe my encapsulation is not broken in any way (this happens a lot in financial software when equations/models take a lot of inputs), then I find the above a very useful pattern.
您甚至可以进一步扩展
ParameterObject
的想法(如 Brian Agnew) 通过使用泛型。这样你的
CoolAlgorithm
需要一个CoolAlgorithmParameter
并且你的NiceAlgorithm
需要一个NiceAlgorithmParameter
所以你不必担心参数对象实际上就是你想要的——泛型会处理这个问题。You can even further extend the idea of a
ParameterObject
(as proposed by Brian Agnew) by using generics.This way your
CoolAlgorithm
takes aCoolAlgorithmParameter
and yourNiceAlgorithm
takes aNiceAlgorithmParameter
so you don’t have to worry about the parameter object actually being what you want—generics will take care of that.