晕了,为何ToString必须加上override

乐百川 乐百川 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
class Test
{
private string str;
public string Str
{
get
{
return str;
}
set
{
str = value;
}
}
public Test()
: this("123")
{

}
public Test(string str)
{
this.str = str;
}
public override string ToString()
{
return Str;
}
/*
* public string ToString()
{
return Str;
}
* public new string ToString()
{
return Str;
}
*/
}
class Program
{
public static void Main(String[] args)
{
Test a, b;
a = new Test();
b = a;
Console.WriteLine("{0} {1} ", a, b);
a.Str = "555";
Console.WriteLine("{0} {1} ", a, b);
}
}

注释掉的那两个,显示结果是名字空间名.加类名,我感觉既然我使用的是Test对象,那么在输出中也应该调用的是我定义的那个,为何会调用object中的Tostring呢,求解
...全文
评论 26
收藏 已收藏
分享
返回顶部
乐百川
C# 111,130 成员 642,539 内容