创建一个具有其他两个对象的所有属性的对象?

发布于 2024-08-21 23:27:29 字数 404 浏览 4 评论 0原文

考虑一个体育俱乐部的情况。 一个俱乐部可以有一名俱乐部经理和一名或多名教练(每队一名教练) 因此,这是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

爱你是孤单的心事 2024-08-28 23:27:29

不,你不能通过类来做到这一点。然而,接口可以让你朝这个方向发展。

另一方面,您可能希望添加另一个间接级别,并将经理和教练角色与人员分开,以便您可以将每个角色分配给一个人(也可能是同一个人)。

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).

空城缀染半城烟沙 2024-08-28 23:27:29

卢塞罗的第二段是正确的做法。教练和经理不是某种人,他们是人扮演的角色。理论上,同一个人可以是团队球员、队长、教练、经理和水男孩!

所以你想要的是组合,而不是继承。例如:

SportsClub myClub = new SportsClub();
Person Bill = new Person();
myClub.Manager = new Manager(Bill);
myClub.Coach = new Coach(Bill);

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:

SportsClub myClub = new SportsClub();
Person Bill = new Person();
myClub.Manager = new Manager(Bill);
myClub.Coach = new Coach(Bill);
離人涙 2024-08-28 23:27:29

作为教练和俱乐部经理并不是这个人的固有属性。它们是该人所扮演的角色。因此,我想说的是,应该为 person 对象(或者无论你如何称呼它)分配角色。然后,这些角色/能力对象将委托这些方法。

所以它可能看起来像这样:

Person joe = new Person("joe");
joe.setClubManager(true);

joe.getManagerAbilities().RegisterMember();

如果没有更多关于您的问题的信息,很难知道什么是正确的,但这也许会让您沿着不同的思路思考。

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:

Person joe = new Person("joe");
joe.setClubManager(true);

joe.getManagerAbilities().RegisterMember();

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.

花桑 2024-08-28 23:27:29

多重继承正是您所寻找的。不幸的是,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.

我家小可爱 2024-08-28 23:27:29

正如 Chinmay Kanchi 指出的,C# 不支持多重继承;但是,您可以从类型继承并实现任意数量的接口。

public interface ICoach {
}

public class ClubManager : SomeType, ICoach {
}

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.

public interface ICoach {
}

public class ClubManager : SomeType, ICoach {
}
[浮城] 2024-08-28 23:27:29

我会使用委托而不是继承,并使用复合模式。 (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)

地狱即天堂 2024-08-28 23:27:29

您可以执行此操作,但需要执行以下过程,因为 C# 不支持通过类继承

Interface IClubManager 
{
  public void RegisterMember();        
}

Interface ICoach 
{
   // code for making a team formation.. 
}
class SomeDesignation : ICoach, IClubManager 
{
}

you can do this but with following procedure as C# does not support Inheritance through classes

Interface IClubManager 
{
  public void RegisterMember();        
}

Interface ICoach 
{
   // code for making a team formation.. 
}
class SomeDesignation : ICoach, IClubManager 
{
}
乖不如嘢 2024-08-28 23:27:29

正如其他人所指出的,尽管您可以使用接口来做到这一点,但您不能使用类来做到这一点,这有一个缺点,即您每次都需要重新实现接口。解决这个问题的一种方法是使用 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#?.

甜扑 2024-08-28 23:27:29

您不可能完全拥有您想要的东西 - 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.

菩提树下叶撕阳。 2024-08-28 23:27:29

这是我脑海中闪现的第一个想法:

  1. 创建一个 ClubManagerCoach 类,或者一些对经理和教练都具有逻辑的效果。
  2. 为 IClubManager 和 ICoach 创建接口。使用这些接口代替 ClubManager 和 Coach。
  3. 让 ClubManagerCoach 实现 IClubManager 和 ICoach。

Here's the first idea that popped into my head:

  1. Create a ClubManagerCoach class, or something to that effect that has logic for both managers and coaches.
  2. Create interfaces for IClubManager and ICoach. Use those interfaces in place of ClubManager and Coach.
  3. Make ClubManagerCoach implement IClubManager and ICoach.
惟欲睡 2024-08-28 23:27:29

我认为在这种情况下(当然,这是一个有争议的话题),您将得到的最好的妥协是创建一些类似于公开 ClubManagerClubManagerCoach 类的东西> 和 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 exposes ClubManager and Coach properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文