纯粹使用继承来共享通用功能 [英] Using inheritance purely to share common functionality

查看:62
本文介绍了纯粹使用继承来共享通用功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在我正在处理的某些代码中遇到了一种情况,这对我来说没有意义.一组类纯粹是从基类继承的,以共享基类中的某些方法.没有方法覆盖,只有子类从父类调用方法.

I recently encountered a situation in some code I am working on that doesn't make sense to me. A set of classes are inheriting from a base class purely to share some methods in the base class. There is no method overriding, just child classes calling methods from the parent class.

在我看来,最好通过让子类引用父类而不是从其继承来更好地建模,我认为这样可以减少不必要的复杂性.这合理吗,还是我错过了使用这样的继承的某些好处?

It seems to me that this would be better modeled by having the child classes reference the parent class rather than inheriting from it, which I think would reduce unnecessary complexity. Is this reasonable, or am I missing some benefit of using inheritance like this?

推荐答案

如果父类方法纯粹是作为实用程序"存在的,那么我同意.

If the parent class methods are there purely as 'utilties' then yes, I agree.

问题(至少对我来说)是,将来是否可以修改父类以获得收益.意思是,当前的关系在逻辑上是什么?如果它是孩子和父母之间的是",则将其保留.如果父级只是方法的集合,则可以重构为实用程序类或使用委托.

The question (for me at least), would be if the parent class could be modified in the future to have benefit. Meaning, what is the current relationship logically? If it's an "is a" between child and parent then leave it. If the parent is just a collection of methods, refactor to a utility class or use delegation.

这篇关于纯粹使用继承来共享通用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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