2

我正在自学 C#,并且有一个关于从父对象的特定实例继承的问题。
例如,如果我这样定义我的类:

class League
{
    public string name { get; set; }
}
class Player : League
{
    public Guid PlayerGuid { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
class Team : Player
{
    public Guid TeamGuid { get; set; }
    public string TeamName { get; set; }
}

然后像这样实例化类:

    static void Main(string[] args)
    {
        League myLeague = new League() { name = "The League" };
        Player taco = new Player() { PlayerGuid = Guid.NewGuid(), FirstName = "Taco", LastName = "MacArthur" };
        Player pete = new Player() { PlayerGuid = Guid.NewGuid(), FirstName = "Pete", LastName = "Eckhart" };
        Team tacoTeam = new Team() { TeamGuid = Guid.NewGuid(), TeamName = "Password is Taco" };
        Team peteTeam = new Team() { TeamGuid = Guid.NewGuid(), TeamName = "3-Pete" };
    }

如何确保 Team tacoTeam 继承自 Player taco 而 Team peteTeam 继承自 Player pete?

我的感觉是,我不了解如何为这种情况正确定义类,并且希望能得到有关如何完成此操作的指针。

为了澄清元素的关系,我将“玩家”更改为“所有者”更好地代表了我的想法。我可以进一步为“玩家”添加一个类来代表真正的玩家而不是扶手椅玩家。

通过@neoistheone 和@McGarnagle 的输入,我能够修改它并这样定义类:

class League
{
    public string name { get; set; }
}
class Owner
{
    public Guid OwnerGuid { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
class Team
{
    public Guid TeamGuid { get; set; }
    public string TeamName { get; set; }
    public Owner TeamOwner { get; set; }
    public League League { get; set; }

}

然后我可以创建类的一个实例并从中得到什么:

    static void Main(string[] args)
    {
        League MyLeague = new League() { name = "The League" };
        Owner taco = new Owner() { FirstName = "Taco", LastName = "MacArthur", OwnerGuid = Guid.NewGuid() };
        Owner pete = new Owner() { FirstName = "Pete", LastName = "Eckhart", OwnerGuid = Guid.NewGuid() };
        Team tacoTeam = new Team() { TeamGuid = Guid.NewGuid(), TeamName = "Password is Taco", League = MyLeague, TeamOwner = taco };
        Team peteTeam = new Team() { TeamGuid = Guid.NewGuid(), TeamName = "3-Pete", League = MyLeague, TeamOwner = pete };

        List<Team> Teams = new List<Team>();
            Teams.Add(tacoTeam);
            Teams.Add(peteTeam);

        foreach(Team team in Teams){
            Console.WriteLine("Team Name:\t{0}\nTeam Owner:\t{1}\nLeague Name:\t{2}\n-----", 
                team.TeamName, team.TeamOwner.FirstName + " " + team.TeamOwner.LastName, team.League.name);
        }

        Console.ReadLine();
    }

这似乎在帮助对我的代码中的数据进行建模方面做得相当不错,以使其更容易进出我正在使用此应用程序开发的数据库。

4

5 回答 5

8

类继承意味着“is-a”关系。例如,您不希望 Player 继承 League,因为这意味着玩家是一个联盟。

相反,你想要的是组合——联赛由球队组成,球队由球员组成。您通过创建班级成员来表达组成。类Team应该有一个成员是类的集合Player,依此类推。例如:

class Player
{
    public Guid PlayerGuid { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

class Team
{
    public Guid TeamGuid { get; set; }
    public string TeamName { get; set; }
    public IList<Player> Players { get; private set; }

    public Team()
    {
        Players = new List<Player>();
    }
}

class League
{
    public string name { get; set; }
    public IList<Team> Teams { get; private set; }

    public League()
    {
        Teams = new List<Team>();
    }
}

现在,为了确保 Taco 属于 Taco 团队,您将他添加到Team类的相应实例中:

static void Main(string[] args)
{
    Player taco = new Player { PlayerGuid = Guid.NewGuid(), FirstName = "Taco", LastName = "MacArthur" };
    Player pete = new Player { PlayerGuid = Guid.NewGuid(), FirstName = "Pete", LastName = "Eckhart" };
    Team tacoTeam = new Team { TeamGuid = Guid.NewGuid(), TeamName = "Password is Taco" };
    Team peteTeam = new Team { TeamGuid = Guid.NewGuid(), TeamName = "3-Pete" };

    tacoTeam.Players.Add(taco);
    peteTeam.Players.Add(pete);

    League myLeague = new League { name = "The League" };
    myLeague.Teams.Add(tacoTeam);
    myLeague.Teams.Add(peteTeam);
}
于 2013-10-08T18:05:32.097 回答
1

退后一步,想想它在现实生活中是如何运作的。ALeague有一个或多个Teams,aTeam有一个或多个Players。这就是您想要构建类的方式:

class League
{
    public string name { get; set; }
    public List<Team> Teams { get; set; }
}

class Team
{
    public Guid TeamGuid { get; set; }
    public string TeamName { get; set; }
    public List<Player> Players { get; set; }
}

class Player
{
    public Guid PlayerGuid { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

所以现在你可以做这样的事情:

var league = new League()
{
    name = "AFC",
    Teams = new List<Team>()
};

var team = new Team()
{
    TeamGuid = Guid.NewGuid(),
    TeamName = "Chicago Bears",
    Players = new List<Player>()
}
league.Teams.Add(team);

var player = new Player()
{
    PlayerGuid = Guid.NewGuid(),
    FirstName = "Test",
    LastName = "Player"
};
team.Players.Add(player);

但就这一点而言:

如何确保 Team tacoTeam 继承自 Player taco 而 Team peteTeam 继承自 Player pete?

这不是继承的目的。继承是为了让您可以扩展Player到例如Linebacker

public class Linebacker : Player
{
    public int Tackles { get; set; }
}

看,不是每个 Player人都有Tackles作为统计数据,而是一个Linebacker会。你可以进一步抽象,但我只是想举个例子。

于 2013-10-08T18:08:17.557 回答
1

这种说法至少可以说很奇怪:

如何确保 Team tacoTeam 继承自 Player taco 而 Team peteTeam 继承自 Player pete?

事实是,继承不一定与您创建的对象有任何关系。Team 继承自 Player……就是这样。

我有点困惑问题来自哪里,但我想指出这种继承很奇怪:

class Team : Player

我看不出一支球队会如何从一名球员那里继承。你确定你应该继承吗?

这样想吧。正如其他人指出的那样,继承意味着“is-a”关系。说一个 Team 是一个 Player 没有任何意义,说一个 Player 是一个 Team 也没有任何意义。团队玩家组成。这并不意味着应该有任何继承。

于 2013-10-08T18:05:39.277 回答
0

我不确定你是否完全理解继承。在这种情况下,真的没有。

一个联赛由一组球队组成,一个球队由一组球员组成。

class League
{
    public string name { get; set; }
    public List<Team> Teams { get; set; }
}

class Team
{
    public Guid TeamGuid { get; set; }
    public string TeamName { get; set; }
    public List<Player> Players { get; set; }
}

class Player
{
    public Guid PlayerGuid { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

在这种情况下,如果您有不同类型的联赛,例如 BaseballLeague 和 FootballLeague,则继承将是,因此在这种情况下,除了具有其他属性外,它们还将从 League 继承,

class BaseballLeague : League
{
    public string SomeOtherProperty { get; set; }
}
于 2013-10-08T18:08:31.760 回答
0

您在此示例中滥用了继承。考虑一个简单的形状示例。

class Shape
{
    Color Color { get; set; }
}

class Rectangle : Shape
{
    double SideA { get; set; }
    double SideB { get; set; }
}

class Square : Rectangle
{
}

你可以看到一个正方形是一个矩形,一个矩形是一个正方形。这三个都有颜色。矩形有两组相等的边。Square 还有一些其他更具体的属性。

您的结构应该遵循以下原则:

class League
{
    public Team[] Teams { get; set; }
}

class Team
{
    public League League { get; set; }
    public Player[] Players { get; set; }
}

class Player
{
    public Team Team { get; set; }
}
于 2013-10-08T18:06:29.063 回答