我想要一个修改现有成员变量并设置修改后的值的类方法。修改和设置类成员变量的类方法
实施例:
public class Animal
{
public string color { get; set; }
public string getColor
{
get
{
return color;
}
}
public string convertColor
{
set
{
this.color = this.colorConversion();
}
}
public string colorConversion()
{
string number = this.getColor;
switch (number)
{
case "0":
return "Lighter";
case "1":
return "Good!";
case "2":
return "Darker";
default:
return "Fix this somehow!";
}
}
}
因此,当我实例化对象,我只需要使用一个线做转换
如:
Animal cow = new Animal();
cow.convertColor;
而不是具有多个线如:
Animal cow = new Animal();
string currentColor = cow.getColor;
cow.setColor = cow.colorConversion(currentColor);
请帮我理解它是否可能。
那些'getColor'和'setColor''方法'是没有必要的 - 'color'属性本身已经有一个getter和setter。而且,忽略属性设置器中给定的“值”不是其他程序员期望的。字符串是否真的是正确的类型? ''Friday“'是一个有效的字符串,但它不是一个有效的颜色......不管怎样,为什么'Animal'需要从数字字符串转换为颜色名称 - 这些数字和颜色名称之间有什么样的关系? –
哎呀,谢谢你抓住这些! :-) 字符串类型是因为我从api获取字符串返回值(一个数字),我需要将它转换为不同格式的字符串。 – wsgb