重寫父類的ToString
我們任何對象調用ToString的時候,打出來的都是這個類的命名空間的名字
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 重寫ToString方法 { class Program { static void Main(string[] args) { Person p = new Person(); Console.WriteLine(p.ToString()); Console.ReadKey(); } } public class Person { } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 重寫ToString方法 { class Program { static void Main(string[] args) { Person p = new Person(); Console.WriteLine(p.ToString()); Console.ReadKey(); } } public class Person { public override string ToString() { return "hello world!";//這里重寫了,只重寫了Person類的ToString方法,然后Person類打出來的ToString就是hello world,但是其他類還是顯示命名空間的名字 } } }

浙公網安備 33010602011771號