我正在努力教自己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();
}
这似乎在帮助对我的代码中的数据进行建模以使其更容易进出我正在使用此应用程序的数据库方面做得相当不错。
答案 0 :(得分:8)
类继承意味着“is-a”关系。例如,你不希望玩家继承联盟,因为这意味着玩家是联盟。
相反,你想要的是组合 - 联盟由球员组成,球队由球员组成。您通过创建类成员来表达组合。类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属于Team 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);
}
答案 1 :(得分:1)
至少可以说这句话很奇怪:
如何确保Team tacoTeam继承自Player taco,而Team peteTeam继承自Player pete?
事实上,继承并不一定与你创建的对象有任何关系。团队继承自Player ......就是这样。
我对这个问题的来源有点困惑,但我想指出这种继承是奇怪的:
class Team : Player
我看不到团队如何从播放器继承。你确定你应该继承吗?
这样想。正如其他人所指出的那样,继承意味着“是一种”关系。说团队是一名球员并没有任何意义,说一名球员是球队也没有意义。团队由玩家组成。这并不意味着应该有任何遗产。
答案 2 :(得分:1)
退后一步,想想它在现实生活中的运作方式。 League
有一个或多个Team
个,Team
有一个或多个Player
个。这就是你想要构建你的类的方式:
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
会。{{1}}。你可以抽象得更多,但我只是想举个例子。
答案 3 :(得分:0)
您在此示例中滥用继承。考虑形状的简单示例。
class Shape
{
Color Color { get; set; }
}
class Rectangle : Shape
{
double SideA { get; set; }
double SideB { get; set; }
}
class Square : Rectangle
{
}
您可以看到Square是一个Rectangle,而Rectangle是一个Square。这三个都有颜色。矩形有两组相等的边。 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; }
}
答案 4 :(得分:0)
我不确定你是否完全理解继承。在这种情况下,实际上并没有。
A League将由一组球队组成,一支球队将由一系列球员组成。
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,所以在那个例子中,除了拥有其他属性之外,他们还会继承联盟,
class BaseballLeague : League
{
public string SomeOtherProperty { get; set; }
}