ToString()方法的使用
Console.WriteLine("Create an Array type collection of Animal objects and use it:");
Animal[] animalArray = new Animal[2];
Cow c1 = new Cow("Tom");
animalArray[0] = c1;
Chicken c2 = new Chicken("sike");
animalArray[1] = c2;
foreach (Animal temp in animalArray)
{
Console.WriteLine(temp.ToString()+" 名字"+temp.Name);
}
Console.WriteLine("数组中包含对象个数为:"+animalArray.Length);
Console.ReadKey();
此处temp.ToString()输出的应该是当前对象的字符串,应该是C1,C2。为什么会输出类名Cow和Chiken。