Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#中的GetSet未被解析_C#_.net_Console_Getter Setter - Fatal编程技术网

C#中的GetSet未被解析

C#中的GetSet未被解析,c#,.net,console,getter-setter,C#,.net,Console,Getter Setter,关于我这里缺少的东西的相对次要的问题, 我试图在C#中执行一个简单的GetSet来掌握语法,但似乎遗漏了一些东西,因为打印的只是GetSet.Role,而不是分配的实际属性。 我刚才说错了什么吗?对于这个小问题表示歉意,但非常感谢您的帮助。 namespace GetSet { class Program { static void Main(string[] args) { Role Mage = new Role("St

关于我这里缺少的东西的相对次要的问题,

我试图在
C#
中执行一个简单的GetSet来掌握语法,但似乎遗漏了一些东西,因为打印的只是
GetSet.Role
,而不是分配的实际属性。

我刚才说错了什么吗?对于这个小问题表示歉意,但非常感谢您的帮助。

namespace GetSet
{
    class Program
    {
        static void Main(string[] args)
        {
            Role Mage = new Role("Staff", "Robes", "Magic affinity");
            Role Warrior = new Role("Sword", "Platebody", "Strength");
            Role Rogue = new Role("Needle", "Leather", "Cunning");

            Console.WriteLine(Mage);
            Console.WriteLine(Warrior);
            Console.WriteLine(Rogue);
            //stop the program from closing
            Console.ReadLine();
        }
    }
}
下面是我的课堂:

namespace GetSet
{
    class Role
    {
        //private variables
        private string weapon;
        private string armour;
        private string passive;

        //public structs
        public Role(string aWeapon, string aArmour, string aPassive)
        {
            weapon = aWeapon;
            armour = aArmour;
            passive = aPassive;
        }

        //Getters and Setters for above private variables
        public string Weapon
        {
            get { return weapon; }
            set { weapon = value;}
        }
        public string Armour
        {
            get { return armour; }
            set { armour = value;}
        }
        public string Passive
        {
            get { return passive; }
            set { passive = value;}
        }
    }
}

您需要重写GetSet类上的
ToString
方法

比如:

public override string ToString() 
{
     return $"{weapon}/{armour}/{passive}";
}

更新 您可以简化您的
角色

internal class Role
{
    public Role(string weapon, string armour, string passive)
    {
        Weapon = weapon;
        Armour = armour;
        Passive = passive;
    }

    public string Weapon { get; }

    public string Armour { get; }

    public string Passive { get; }

    public override string ToString()
    {
        return $"{Weapon}/{Armour}/{Passive}";
    }
}

回复:瓦西里。sib的评论

如果需要在对象创建后更改属性,只需更改

public string Passive { get; }


您需要重写GetSet类上的
ToString
方法

比如:

public override string ToString() 
{
     return $"{weapon}/{armour}/{passive}";
}

更新 您可以简化您的
角色

internal class Role
{
    public Role(string weapon, string armour, string passive)
    {
        Weapon = weapon;
        Armour = armour;
        Passive = passive;
    }

    public string Weapon { get; }

    public string Armour { get; }

    public string Passive { get; }

    public override string ToString()
    {
        return $"{Weapon}/{Armour}/{Passive}";
    }
}

回复:瓦西里。sib的评论

如果需要在对象创建后更改属性,只需更改

public string Passive { get; }

ToString()
添加到您的角色类中,并将其设置为返回您想要的任何内容:

    public override string ToString()
    {
        return $"Weapon: {weapon}, Armor: {armor}, Passive: {passive}";
    } 
ToString()
添加到您的角色类中,并将其设置为返回您想要的任何内容:

    public override string ToString()
    {
        return $"Weapon: {weapon}, Armor: {armor}, Passive: {passive}";
    } 

由于其他答案缺少getter/setter语法示例,我将发布我的答案

namespace GetSet
{
    public class Role
    {
        // private backing field
        private string _weapon;

        // properties can have getters and setters, that contains some logic
        public string Weapon
        {
            get { return _weapon; }
            set { if (_weapon != vale) _weapon = value; }
        }

        // there is an auto-getters/setters
        // in this case, backing field is handled by .Net CLR
        public string Armour { get; set; }

        // getters and setters may have different access level
        // also, note property initializer '= "John";' - this will set property value
        // to "John" right before constructor invocation
        public string Name { get; private set; } = "John";

        // properties also can be readonly, so they can be setted only in constructors
        public string Passive { get; }

        // public constructor
        public Role(string passive)
        {
            Passive = passive;
        }

        public void ChangeName(string newName)
        {
            Name = newName; // setting property through private setter
        }

        // I believe, that this method shouldn't be used to represent object as string
        // At least, I think, you should never relay on it's return value, BUT it ups to you
        public overide string ToString() => Name;
    }
}
另外,如您所见,我没有在Consturators中设置公共可用属性(带有公共设置器的属性,
武器
装甲
),因为我可以在构造
角色
对象的同时初始化它们,如下所示:

var mage = new Role("Magic affinity") { Weapon = "Staff", Armor = "Robes" };
mage.ChangeName("John, Doe");
正如前面所说的,我相信它不是对象本身的中继,而是它在字符串中的外观。我这样认为,因为如果出于某些原因需要在代码的不同位置将同一对象表示为不同的字符串,这将导致麻烦。因此,与此相反:

// this will call .ToString() method
Console.WriteLine(mage);
// output: John, Doe
我建议:

// represent object as you need
Console.WriteLine($"{mage.Name} - walks in {mage.Armour}, beats with {mage.Weapon}");
// output: John, Doe - walks in Robes, beats with Staff

由于其他答案缺少getter/setter语法示例,我将发布我的答案

namespace GetSet
{
    public class Role
    {
        // private backing field
        private string _weapon;

        // properties can have getters and setters, that contains some logic
        public string Weapon
        {
            get { return _weapon; }
            set { if (_weapon != vale) _weapon = value; }
        }

        // there is an auto-getters/setters
        // in this case, backing field is handled by .Net CLR
        public string Armour { get; set; }

        // getters and setters may have different access level
        // also, note property initializer '= "John";' - this will set property value
        // to "John" right before constructor invocation
        public string Name { get; private set; } = "John";

        // properties also can be readonly, so they can be setted only in constructors
        public string Passive { get; }

        // public constructor
        public Role(string passive)
        {
            Passive = passive;
        }

        public void ChangeName(string newName)
        {
            Name = newName; // setting property through private setter
        }

        // I believe, that this method shouldn't be used to represent object as string
        // At least, I think, you should never relay on it's return value, BUT it ups to you
        public overide string ToString() => Name;
    }
}
另外,如您所见,我没有在Consturators中设置公共可用属性(带有公共设置器的属性,
武器
装甲
),因为我可以在构造
角色
对象的同时初始化它们,如下所示:

var mage = new Role("Magic affinity") { Weapon = "Staff", Armor = "Robes" };
mage.ChangeName("John, Doe");
正如前面所说的,我相信它不是对象本身的中继,而是它在字符串中的外观。我这样认为,因为如果出于某些原因需要在代码的不同位置将同一对象表示为不同的字符串,这将导致麻烦。因此,与此相反:

// this will call .ToString() method
Console.WriteLine(mage);
// output: John, Doe
我建议:

// represent object as you need
Console.WriteLine($"{mage.Name} - walks in {mage.Armour}, beats with {mage.Weapon}");
// output: John, Doe - walks in Robes, beats with Staff

请在问题中发布代码本身,而不是图片。啊,好的,我会在里面编辑它,抱歉,还有一些评论用错了词。您所调用的
私有变量
实际上是
私有字段
,如果它们用作属性的值存储(如在您的代码中),我们将它们称为
后备字段
。您所调用的
public structs
是一个
public构造函数
struct
s是完全不同的东西。请在问题中发布代码本身,而不是图片。啊,好的,我会在其中编辑它,抱歉,还有一些注释用词错误。您所调用的
私有变量
实际上是
私有字段
,如果它们用作属性的值存储(如在您的代码中),我们将它们称为
后备字段
。您所调用的
public structs
是一个
public构造函数
struct
s是完全不同的东西。我对简化表示怀疑,因为有问题的代码中有公共setter,所以我认为只读属性在这里不是一个选项:\@vasily.sib发布的代码不使用setter。(顺便说一句,这就是原始示例代码起作用的原因-setters中有一个bug。)不确定你在说什么bug,但由于Ben正在尝试了解getter和setters的一些知识,我很确定他需要一些setters@vasily.sib原始代码是“set{Passive=value;}”(注意大写字母“P”)。当你说“非常确定”的时候,你是基于什么的?我是基于一个逻辑,我不能毫无疑问地了解带有代码的setter。我怀疑简化,因为有问题的代码中有公共setter,所以我认为只读属性在这里不是一个选项:\@vasily.sib发布的代码不使用setter。(顺便说一句,这就是原始示例代码起作用的原因-setters中有一个bug。)不确定你在说什么bug,但由于Ben正在尝试了解getter和setters的一些知识,我很确定他需要一些setters@vasily.sib原始代码是“set{Passive=value;}”(注意大写字母“P”)。当你说“非常确定”的时候,你是基于什么的?我是基于一个逻辑,我不能学习一些关于没有setter的代码的setter