如何从两个类继承 [英] How to inherit from two classes

查看:68
本文介绍了如何从两个类继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个People类,以及从其继承的StudentEmployee类.但是如果我有一个既是Student又是Employee ...

I have a People class, and Student and Employee classes which inherit from it. But if I have a person who is both a Student and an Employee...

...您将如何实现这一目标?

... How would you go about implementing this?

推荐答案

这是对问题域进行了不正确分析的经典示例.是的,在某些情况下,将学生"视为人"类型,将雇员"视为人"类型可能是适当的,但是-根据您的问题领域-也可能不合适

That's a classic example of an improperly analyzed problem domain. Yes, in some situations, it may be proper to think of "Student" as a type of "Person" and an "Employee" as a type of "Person", but - depending on your problem domain - it may also not be appropriate.

如果您的域要求某人既是学生"又是雇员",则应考虑问题域中学生"和人"之间的关系是否确实是"is-a"的关系.

If your domain requires that something be both a "Student" and an "Employee", you should consider if the relation between "Student" and "Person" in your problem domain is really an "is-a" relationship.

在这种特殊情况下,成为一名学生可能只是特定人的属性.因此,John Doe是一个人,可能还拥有学生"的当前职业".在这种情况下,他可能会列出几个当前职业".这样的世界中的关系就变成了有一个"而不是是一个".因此,学生"和员工"成为职业"的子类.

It could be that in this particular case, being a student is merely an attribute of a particular person. So, John Doe is a person, who may also have a "current occupation" of "Student". In this case, he may have a list of several "current occupations". And the relationship in such a world becomes "has-a" rather than "is-a". So "Student" and "Employee" become subclasses of "Occupation".

这篇关于如何从两个类继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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