加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#类的数组

发布时间:2020-12-15 07:48:47 所属栏目:百科 来源:网络整理
导读:我正在尝试创建一个控制台游戏,您可以在其中输入多达24个名称.为此,我创建了一个名为PlayerDataAr []的类的数组,其中包含24个元素.它提示用户输入一些名称,并将这些名称分配给数组中的每个元素,其中字符串Name和bool的值为Alive但由于某种原因,我在分配它们
我正在尝试创建一个控制台游戏,您可以在其中输入多达24个名称.为此,我创建了一个名为PlayerDataAr []的类的数组,其中包含24个元素.它提示用户输入一些名称,并将这些名称分配给数组中的每个元素,其中字符串Name和bool的值为Alive但由于某种原因,我在分配它们时似乎无法访问这些值玩家;选手.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace hG
{
    public class PlayerData
    {
        private static bool _isAlive;
        public static bool isAlive
        {
            get { return _isAlive;}
            set { _isAlive = value;}
        }

        private static string _Name;
        public static string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        public PlayerData()
        {
            _isAlive = false;
            _Name = "";
        }

        public static void SetisAlive(bool a)
        {
            _isAlive = a;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string EnterName = "";

            //Array of Player Data
            PlayerData[] PlayerDataAr = new PlayerData[24];
            for (int x = 0; x < 24; x++ )
            {
                PlayerDataAr[x] = new PlayerData();
            }

            //Set up the Console
            Console.Title = "Hunger Games";
            Console.SetBufferSize(100,42);
            Console.SetWindowSize(100,42);

            Console.BackgroundColor = ConsoleColor.Yellow;
            Console.Clear();

            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.BackgroundColor = ConsoleColor.DarkRed;
            Console.WriteLine("Welcome");
            Console.BackgroundColor = ConsoleColor.Yellow;
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Enter the names for tributes and press enter when done:");

            //Loop through collecting names
            for(int x = 0; x < 25; x++)
            {
                Console.Write("--> ");
                EnterName = Console.ReadLine();
                if (EnterName == "")
                {
                    break;
                }
                else
                {
                    //Assign Player Data
                    PlayerDataAr[x].Name = EnterName;        //Error appears here
                    PlayerDataAr[x].isAlive = true;          
                }                
            }
            Console.Clear();

            for (int x = 0; x < 24; x++)
            {

            }

            //Start Game
            while(true)
            {
                Console.ReadLine();
            }    
        }
    }

}

它返回:

Member ‘hG.PlayerData.isAlive.get’ cannot be accessed with an instance reference; qualify it with a type name instead.

我不知道它在谈论它的名字和isAlive.
任何帮助将不胜感激.

解决方法

从PlayerData中删除静态:
public class PlayerData
{
    private bool _isAlive;
    public bool isAlive
    {
        get { return _isAlive;}
        set { _isAlive = value;}
    }

    private string _Name;
    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }

    public PlayerData()
    {
        _isAlive = false;
        _Name = "";
    }

    public void SetisAlive(bool a)
    {
        _isAlive = a;
    }
}

更好的设计是使用自动实现的属性:

public class PlayerData 
{
    public bool isAlive
    {
        get;
        set;
    }

    public string Name
    {
        get;
        set;
    }

    public PlayerData()
    {
        isAlive = false; // redundant isAlive == false by default
        Name = "";
    }

    // redundant: you can easily put isAlive = value;
    public void SetisAlive(bool value)
    {
        isAlive = value;
    }
}

static表示基于类:PlayerData作为一个整体具有单一的isAlive属性值.您需要不同的行为:每个PlayerData实例都有自己的属性值,这就是为什么isAlive应该只是一个实例属性(没有静态).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读