《C#入门经典》第十一章1.1代码using System;namespace Ch11Ex01
{
/// <summary>
/// Summary description for Animal.
/// </summary>
public abstract class Animal
{
protected string name; public string Name
{
get
{
return name;
}
set
{
name = value;
}
} public Animal()
{
name = "The animal with no name";
} public Animal(string newName)
{
name = newName;
} public void Feed()
{
Console.WriteLine("{0} has been fed.", name);
}
}
public class Cow : Animal
{
public void Milk()
{
Console.WriteLine("{0} has been milked.", name);
} public Cow(string newName) : base(newName)
{
}
} public class Chicken : Animal
{
public void LayEgg()
{
Console.WriteLine("{0} has laid an egg.", name);
} public Chicken(string newName) : base(newName)
{
}
}
}
using System;
using System.Collections;namespace Ch11Ex01
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Create an Array type collection of Animal " +
"objects and use it:");
Animal[] animalArray = new Animal[2];
Cow myCow1 = new Cow("Deirdre");
animalArray[0] = myCow1;
animalArray[1] = new Chicken("Ken");
foreach (Animal myAnimal in animalArray)
{
Console.WriteLine("New {0} object added to Array collection, " +
"Name = {1}", myAnimal.ToString(), myAnimal.Name);
}
Console.WriteLine("Array collection contains {0} objects.",
animalArray.Length);
animalArray[0].Feed();
((Chicken)animalArray[1]).LayEgg();
Console.WriteLine(); Console.WriteLine("Create an ArrayList type collection of Animal " +
"objects and use it:");
ArrayList animalArrayList = new ArrayList();
Cow myCow2 = new Cow("Hayley");
animalArrayList.Add(myCow2);
animalArrayList.Add(new Chicken("Roy"));
foreach (Animal myAnimal in animalArrayList)
{
Console.WriteLine("New {0} object added to ArrayList collection," +
" Name = {1}", myAnimal.ToString(), myAnimal.Name);
}
Console.WriteLine("ArrayList collection contains {0} objects.",
animalArrayList.Count);
((Animal)animalArrayList[0]).Feed();
((Chicken)animalArrayList[1]).LayEgg();
Console.WriteLine(); Console.WriteLine("Additional manipulation of ArrayList:");
animalArrayList.RemoveAt(0);
((Animal)animalArrayList[0]).Feed();
animalArrayList.AddRange(animalArray);
((Chicken)animalArrayList[2]).LayEgg();
Console.WriteLine("The animal called {0} is at index {1}.",
myCow1.Name, animalArrayList.IndexOf(myCow1));
myCow1.Name = "Janice";
Console.WriteLine("The animal is now called {0}.",
((Animal)animalArrayList[1]).Name); }
}
}在class类中,有这样的语句
animalArray[0].Feed();
((Chicken)animalArray[1]).LayEgg();
问题一:为什么第一个语句不用类型,而第二个要呢
问题二:用类来做类型和用类来做类型转换是什么意思,是不是和结构类似?
问题三:怎样才能用类做类型转换?
问题四:怎样书上都没说过这个问题呢?是我看漏了吗?
{
/// <summary>
/// Summary description for Animal.
/// </summary>
public abstract class Animal
{
protected string name; public string Name
{
get
{
return name;
}
set
{
name = value;
}
} public Animal()
{
name = "The animal with no name";
} public Animal(string newName)
{
name = newName;
} public void Feed()
{
Console.WriteLine("{0} has been fed.", name);
}
}
public class Cow : Animal
{
public void Milk()
{
Console.WriteLine("{0} has been milked.", name);
} public Cow(string newName) : base(newName)
{
}
} public class Chicken : Animal
{
public void LayEgg()
{
Console.WriteLine("{0} has laid an egg.", name);
} public Chicken(string newName) : base(newName)
{
}
}
}
using System;
using System.Collections;namespace Ch11Ex01
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Create an Array type collection of Animal " +
"objects and use it:");
Animal[] animalArray = new Animal[2];
Cow myCow1 = new Cow("Deirdre");
animalArray[0] = myCow1;
animalArray[1] = new Chicken("Ken");
foreach (Animal myAnimal in animalArray)
{
Console.WriteLine("New {0} object added to Array collection, " +
"Name = {1}", myAnimal.ToString(), myAnimal.Name);
}
Console.WriteLine("Array collection contains {0} objects.",
animalArray.Length);
animalArray[0].Feed();
((Chicken)animalArray[1]).LayEgg();
Console.WriteLine(); Console.WriteLine("Create an ArrayList type collection of Animal " +
"objects and use it:");
ArrayList animalArrayList = new ArrayList();
Cow myCow2 = new Cow("Hayley");
animalArrayList.Add(myCow2);
animalArrayList.Add(new Chicken("Roy"));
foreach (Animal myAnimal in animalArrayList)
{
Console.WriteLine("New {0} object added to ArrayList collection," +
" Name = {1}", myAnimal.ToString(), myAnimal.Name);
}
Console.WriteLine("ArrayList collection contains {0} objects.",
animalArrayList.Count);
((Animal)animalArrayList[0]).Feed();
((Chicken)animalArrayList[1]).LayEgg();
Console.WriteLine(); Console.WriteLine("Additional manipulation of ArrayList:");
animalArrayList.RemoveAt(0);
((Animal)animalArrayList[0]).Feed();
animalArrayList.AddRange(animalArray);
((Chicken)animalArrayList[2]).LayEgg();
Console.WriteLine("The animal called {0} is at index {1}.",
myCow1.Name, animalArrayList.IndexOf(myCow1));
myCow1.Name = "Janice";
Console.WriteLine("The animal is now called {0}.",
((Animal)animalArrayList[1]).Name); }
}
}在class类中,有这样的语句
animalArray[0].Feed();
((Chicken)animalArray[1]).LayEgg();
问题一:为什么第一个语句不用类型,而第二个要呢
问题二:用类来做类型和用类来做类型转换是什么意思,是不是和结构类似?
问题三:怎样才能用类做类型转换?
问题四:怎样书上都没说过这个问题呢?是我看漏了吗?
2 不是。 一定要有继承关系才可以转换
3 class A{} class B:A{} B继承于A 就可以转换了 A A1=new B(); B A2=(B)A1;类似于这样
4 估计是你看漏了
myAnimal.Name则体现了基类的抽象功能
引用指向Chicken类的对象。因为Cow类继承类父类的Feed()方法,所以可以直接
animalArray[0].Feed(); 这样调用,而LayEgg(); 是子类Chicken特有的方法,父类没有,所以要转换。2,类是引用类型,结构是值类型。面向对象的变成中,我们要访问某个类的成员的时候,要通过 对象.成员
这种方式来访问,那么这个对象就是由类来定义的。3,类的继承中,当我们初始化一个父类对象。那么该父类对象可以通过类型转换,来转换为任何其子类的对象4,可能书上是用其他方式来表达的,你没注意到。