【问题标题】:Creating a collection that stores objects of derived classes [duplicate]创建一个存储派生类对象的集合
【发布时间】:2021-05-22 22:46:14
【问题描述】:

我正在创建一个基于 .NET core 菜单的控制台应用程序,它允许您添加和管理来自不同运动项目的球员。

我的播放器类:

abstract class Player
    {
        enum PlayerType
        {
            HockeyPlayer,
            BacketballPlayer,
            BaseballPlayer
        }
        public abstract void Points();
        private long playerId;
        private string playerName;
        private string teamName;
        private int gamesPlayed;

        public long PlayerId
        {
            get { return playerId; }
            set { playerId = value; }
        }
        public string PlayerName
        {
            get { return playerName; }
            set { playerName = value; }
        }
        public string TeamName
        {
            get { return teamName; }
            set { teamName = value; }
        }
        public int GamesPlayed
        {
            get { return gamesPlayed; }
            set { gamesPlayed = value; }
        }
    }
    class HockeyPlayer : Player
    {
        private int assists;
        private int goals;

        public override void Points()
        {
            int totalPoints = assists + (2 * goals);
        }
        public int Assists
        {
            get { return assists; }
            set { assists = value; }
        }
        public int Goals
        {
            get { return goals; }
            set { goals = value; }
        }

    }
    class BasketballPlayer : Player
    {
        private int fieldGoals;
        private int threePointers;
        public override void Points()
        {
            int totalPoints = (fieldGoals - threePointers) + (2 * threePointers);
        }
        public int FieldGoals
        {
            get { return fieldGoals; }
            set { fieldGoals = value; }
        }
        public int ThreePointer
        {
            get { return threePointers; }
            set { threePointers = value; }
        }
    }
    class BaseballPlayer : Player
    {
        private int runs;
        private int homeRuns;
        public override void Points()
        {
            int totalPoints = (runs - homeRuns) + (2 * homeRuns);
        }
        public int Runs
        {
            get { return runs; }
            set { runs = value; }
        }
    }

还有我的主要课程的 sn-p:

class Controller
    {
        static void Main(string[] args)
        {

            List<Player> players = new List<Player>()
            {
                new HockeyPlayer(1, "Mitch Marner", "Toronto Maple Leafs", 5)
            };

在控制器中,我试图用一些示例数据填充列表,但显然我在派生的 HockeyPlayer 类中没有构造函数。如何为 HockeyPlayer 创建一个构造函数,其中参数是来自父类的“playerId”、“playerName”、“teamName”和“gamesPlayed”。我觉得我在这里遗漏了一些非常简单的东西。

【问题讨论】:

  • How do I create a constructor for HockeyPlayer where the arguments 你能告诉我们你的尝试吗?

标签: c# .net inheritance collections constructor


【解决方案1】:

有趣的是,您对列表使用“对象初始化”,但尝试调用 HockeyPlayer 的构造函数,您也可以在其中使用对象初始化方法。

对象初始化在构造函数(分配属性)之后执行,但可以在一条语句中进行构造和属性初始化

例如:

List<Player> players = new List<Player>()
{
    new HockeyPlayer(1, "Mitch Marner", "Toronto Maple Leafs", 5);
};

List<Player> players = new List<Player>()
{
    new HockeyPlayer
    {
        PlayerId = 1, 
        PlayerName = "Mitch Marner", 
        TeamName = "Toronto Maple Leafs", 
        GamesPlayed = 5
    }
};

当某些参数是必需的时,构造函数很有用。这样您就可以强制为字段/属性设置初始值。

对象初始化器只是构造和分配属性的语法糖。有很大的不同(就像我说的那样),对象初始化器可以被视为单个语句。 (因此您可以将其作为参数传递给函数。)

var player = new HockeyPlayer
{
    PlayerId = 1, 
    PlayerName = "Mitch Marner", 
    TeamName = "Toronto Maple Leafs", 
    GamesPlayed = 5
}

VS

var player = new HockeyPlayer();
player.PlayerId = 1; 
player.PlayerName = "Mitch Marner"; 
player.TeamName = "Toronto Maple Leafs";
player.GamesPlayed = 5;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 2020-08-06
    • 2020-03-17
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    相关资源
    最近更新 更多