用类做的类型转换是什么意思?

proyeah 2009-08-02 01:10:44
《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();
问题一:为什么第一个语句不用类型,而第二个要呢
问题二:用类来做类型和用类来做类型转换是什么意思,是不是和结构类似?
问题三:怎样才能用类做类型转换?
问题四:怎样书上都没说过这个问题呢?是我看漏了吗?
...全文
155 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hecker728 2009-08-03
  • 打赏
  • 举报
回复
1,animalArray是父类对象。animalArray[0]对象的引用指向Cow类的对象,animalArray[1]对象的
 引用指向Chicken类的对象。因为Cow类继承类父类的Feed()方法,所以可以直接
animalArray[0].Feed(); 这样调用,而LayEgg(); 是子类Chicken特有的方法,父类没有,所以要转换。

2,类是引用类型,结构是值类型。面向对象的变成中,我们要访问某个类的成员的时候,要通过 对象.成员
这种方式来访问,那么这个对象就是由类来定义的。

3,类的继承中,当我们初始化一个父类对象。那么该父类对象可以通过类型转换,来转换为任何其子类的对象

4,可能书上是用其他方式来表达的,你没注意到。

微创社(MCC) 2009-08-03
  • 打赏
  • 举报
回复
简单工厂模式

说明:继承的is-a关系

包括隐式转换,如:(Chicken)animalArray[1]等
myAnimal.Name则体现了基类的抽象功能
yuanhuiqiao 2009-08-03
  • 打赏
  • 举报
回复
这里面有一定的解耦思想
jimh 2009-08-03
  • 打赏
  • 举报
回复
up
xiaojia19850414 2009-08-02
  • 打赏
  • 举报
回复
占位学习。
懦芞 2009-08-02
  • 打赏
  • 举报
回复
up
fengjian_428 2009-08-02
  • 打赏
  • 举报
回复
1 因为Chicken子类和animal父类都有Feed方法 而LayEgg方法只有Chicken子类具有
2 不是。 一定要有继承关系才可以转换
3 class A{} class B:A{} B继承于A 就可以转换了 A A1=new B(); B A2=(B)A1;类似于这样
4 估计是你看漏了

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧