You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#如何在子类方法中使用父类变量及代码报错修复

问题根因
  • CS1061报错是拼写错误:代码中多处把成员名noOfPlayers误写为onOfPlayers
  • CS0122报错是访问权限问题:Team类的teamName、noOfPlayers默认是private私有级别,子类和外部Main方法都没有访问权限
  • 除此之外原代码还存在成员方法逻辑错误、ChangeTeamName未实现、语法符号错误等问题
修正后完整代码
namespace ConsoleApp1
{
    public partial class Program
    {
        public class Team
        {
            // 修改访问修饰符为protected internal,子类、同程序集类都可访问
            protected internal string teamName;
            protected internal int noOfPlayers;

            public Team(string teamName, int noOfPlayers)
            {
                this.teamName = teamName;
                this.noOfPlayers = noOfPlayers;
            }
            // 修正AddPlayer逻辑,完成累加操作
            public void AddPlayer(int count)
            {
                noOfPlayers += count;
            }

            // 修正RemovePlayer逻辑:先判断再操作,符合需求
            public bool RemovePlayer(int count)
            {
                if (noOfPlayers - count < 0)
                {
                    return false;
                }
                noOfPlayers -= count;
                return true;
            }
        }

        class Subteam : Team
        {
            public Subteam(string teamName, int noOfPlayers) : base(teamName, noOfPlayers)
            {

            }

            // 实现修改队名逻辑
            public void ChangeTeamName(string name)
            {
                this.teamName = name;
            }
        }
        static void Main(string[] args)
        {
            if (!typeof(Subteam).IsSubclassOf(typeof(Team)))
            {
                throw new Exception("Subteam class should inherit from Team class");
            }

            String str = Console.ReadLine();
            String[] strArr = str.Split();
            string initialName = strArr[0];
            int count = Convert.ToInt32(strArr[1]);
            Subteam teamObj = new Subteam(initialName, count);
            Console.WriteLine("Team " + teamObj.teamName + " created");

            str = Console.ReadLine();
            count = Convert.ToInt32(str);
            // 修正语法错误,把逗号改成点
            Console.WriteLine("Current number of players in team " + teamObj.teamName + " is " + teamObj.noOfPlayers);
            teamObj.AddPlayer(count);
            // 修正拼写错误onOfPlayers->noOfPlayers
            Console.WriteLine("New number of players in team " + teamObj.teamName + " is " + teamObj.noOfPlayers);

            str = Console.ReadLine();
            count = Convert.ToInt32(str);
            Console.WriteLine("Current number of players in team " + teamObj.teamName + " is " + teamObj.noOfPlayers);
            var res = teamObj.RemovePlayer(count);
            if (res)
            {
                Console.WriteLine("New number of players in team " + teamObj.teamName + " is " + teamObj.noOfPlayers);
            }
            else
            {
                Console.WriteLine("Number of players in team " + teamObj.teamName + " remains same");
            }

            str = Console.ReadLine();
            teamObj.ChangeTeamName(str);
            Console.WriteLine("Team name of team " + initialName + " changed to " + teamObj.teamName);
        }
    }
}
核心修改说明
  • 调整Team类成员变量的访问级别,解决访问权限报错
  • 修正所有拼写错误与语法符号错误
  • 重写AddPlayer、RemovePlayer方法,完全匹配需求逻辑
  • 实现ChangeTeamName方法,完成父类teamName字段修改
  • 调整RemovePlayer的判断逻辑,先判断结果是否合法再执行修改,避免出现负数值

内容的提问来源于stack exchange,提问作者Sandanuwan Dharmarathna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 08:45:07