创建一个具有其他两个对象的所有属性的对象?
考虑一个体育俱乐部的情况。 一个俱乐部可以有一名俱乐部经理和一名或多名教练(每队一名教练) 因此,这是 Manager 和 Coach 类的示例。
Class ClubManager
{
public void RegisterMember()
{
// code for registering a member..
}
}
Class Coach
{
public void DeviceTeamFormation()
{
// code for making a team formation..
}
}
我的问题是,我怎样才能拥有一位兼任教练的俱乐部经理?因为有体育俱乐部有这种情况。
感谢任何和所有的帮助。 谢谢你们。
Consider a sports club situation.
A club can have a Club manager and 1 or more Coaches (1 coach per team)
So, here is an example of the Manager and Coach classes.
Class ClubManager
{
public void RegisterMember()
{
// code for registering a member..
}
}
Class Coach
{
public void DeviceTeamFormation()
{
// code for making a team formation..
}
}
My issue is, how can I have a Club manager who is also a Coach? because there are sports clubs with this situation.
Appreciate any and all help.
Thanks guys.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
不,你不能通过类来做到这一点。然而,接口可以让你朝这个方向发展。
另一方面,您可能希望添加另一个间接级别,并将经理和教练角色与人员分开,以便您可以将每个角色分配给一个人(也可能是同一个人)。
No, you cannot do this with classes. Interfaces however get you in that direction.
On the other hand, you may want to add another level of indirection and separate the manager and coach roles from persons, so that you can assign each role to a person (which may also be the same person).
卢塞罗的第二段是正确的做法。教练和经理不是某种人,他们是人扮演的角色。理论上,同一个人可以是团队球员、队长、教练、经理和水男孩!
所以你想要的是组合,而不是继承。例如:
Lucero's second paragraph is the correct approach. Coach and Manager are not kinds of people, they are roles that people play. Theoretically the same person could be a TeamPlayer, TeamCaptain, Coach, Manager, and WaterBoy!
So what you want is composition, not inheritance. For example:
作为教练和俱乐部经理并不是这个人的固有属性。它们是该人所扮演的角色。因此,我想说的是,应该为 person 对象(或者无论你如何称呼它)分配角色。然后,这些角色/能力对象将委托这些方法。
所以它可能看起来像这样:
如果没有更多关于您的问题的信息,很难知道什么是正确的,但这也许会让您沿着不同的思路思考。
Being a Coach and being a ClubManager are not inherent attributes of this person. They are roles that the person has taken on. As such I would say that the person object (or whatever it is you want to call it) should be assigned roles. Those role/ability objects would then delegate those methods.
So it might look like this:
Hard to know what is right without a lot more information about your problem, but maybe this will get you thinking along some different line.
多重继承正是您所寻找的。不幸的是,C# 目前不支持此功能。我也没有看到它在 2010 版本中出现。
您可以实现多个接口,但不能从多个类继承。
Multiple Inheritance is what you are looking for. Unfortunately, this is not currently supported in C#. I did not see it come up in the 2010 version either.
You can implement multiple interfaces, but you cannot inherit from multiple classes.
正如 Chinmay Kanchi 指出的,C# 不支持多重继承;但是,您可以从类型继承并实现任意数量的接口。
As Chinmay Kanchi noted, C# does not support multiple inheritance; however, you can inherit from a type and implement as many interfaces as you like.
我会使用委托而不是继承,并使用复合模式。 (http://en.wikipedia.org/wiki/Composite_pattern)
I would use delegation rather than inheritance and use the Composite Pattern instead. (http://en.wikipedia.org/wiki/Composite_pattern)
您可以执行此操作,但需要执行以下过程,因为 C# 不支持通过类继承
you can do this but with following procedure as C# does not support Inheritance through classes
正如其他人所指出的,尽管您可以使用接口来做到这一点,但您不能使用类来做到这一点,这有一个缺点,即您每次都需要重新实现接口。解决这个问题的一种方法是使用 mixins: Is it possible to Implement mixins in C# ?。
As others have noted, you can't do that with classes, although you can do it with interfaces, which has the disadvantage that you have reimplement the interface each time. One way around that is with mixins: Is it possible to implement mixins in C#?.
您不可能完全拥有您想要的东西 - C# 不支持多重继承。
与有关使用接口的其他建议(及其局限性)一起,您完全可以重新考虑对象模型。
如果您分离数据结构(类),因此 Coach 类和 ClubManager 类具有 Person 属性,那么您将不会“重复”该人的数据。您可以使用工厂方法来创建 Coach 和/或 ClubManager 。
另一种选择是具有“InRole”功能,该功能说明一个人可以具有什么角色,并且 Person 类将具有您需要的两种方法,但如果该人不处于正确的角色,则每个方法都可能抛出异常执行操作。
或者,您可以注入“角色”,并使用某种形式的间接访问特定角色的功能。
You can not have exactly what you want - multiple inheritance is not supported in C#.
Along with the other proposals about using interfaces (with their limitations), you can possibly re-think the object model at all.
If you separate your data structure (classes), so the Coach class and the ClubManager class has property Person, they you will not "duplicate" the person's data. And you can have factory methods to create Coach out of person, and/or ClubManager out of person.
Another option would be to have "InRole" functionality, which says what role a person can have, and the Person class will have both methods you require, but each one of them may throw an exception if the person is not in the right role to perform the operation.
Or, you can inject "roles", and use some form of indirection to access the specific role's functionality.
这是我脑海中闪现的第一个想法:
Here's the first idea that popped into my head:
我认为在这种情况下(当然,这是一个有争议的话题),您将得到的最好的妥协是创建一些类似于公开
ClubManager
ClubManagerCoach 类的东西> 和Coach
属性。I think the best compromise you'll get in this case (and this is a debated topic, of course) is to create something along the lines of a
ClubManagerCoach
class that exposesClubManager
andCoach
properties.