关于我在这里缺少的东西的相对较小的问题,
我正在尝试在C#
中执行一个简单的GetSet来获取语法的内容,但是由于打印的所有内容都是GetSet.Role
而不是实际的属性,因此似乎遗漏了某些内容。 br />
我刚才说错了什么吗?对于这个小问题,我们深表歉意,但对您的帮助也深表感谢。
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;}
}
}
}
答案 0 :(得分:1)
您需要重写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; }
答案 1 :(得分:1)
向您的Role类添加../results/${__time(MMddyyyyhhmm,)}aggReport.csv
并将其设置为返回所需内容:
ToString()
答案 2 :(得分:0)
由于其他答案缺乏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;
}
}
此外,如您所见,我没有在构造函数中设置公共可用的属性(具有公共设置器,Weapon
和Armour
的属性),因为我可以在构造{{1 }}对象,如下所示:
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