簡體   English   中英

C#中的GetSet未被解析

[英]GetSet in C# not being parsed

關於我在這里缺少的東西的相對較小的問題,

我試圖在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}";
}

更新資料

您可以簡單地Role類。

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}";
    }
}

回復:vasily.sib的評論。

如果在對象創建后需要更改屬性,則只需更改

public string Passive { get; }

public string Passive { get; set; }

向您的Role類添加ToString()並將其設置為返回所需內容:

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

由於其他答案缺少getters / setters語法示例,因此我將發布我的文章。

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;
    }
}

另外,如您所見,我沒有在構造器中設置公共可用的屬性(具有公共設置器的屬性WeaponArmour ),因為我可以像構造Role對象那樣初始化它們:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM