我的兄弟为我写了这段代码来展示一个多态的例子。但我真的不明白它是如何工作的,从参数开始。有人可以剖析代码并告诉我它是如何一起工作的以及它是如何运作的吗?
编辑:这是一个具体的问题:mamams.Add(new Cat("Snow white", 1, 0, "White", "blue")); 数字有什么作用?他们的目的是什么以及他们如何使用代码?
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
List<Mammal> mammals = new List<Mammal>();
mammals.Add(new Cat("Snow white", 1, 0, "White", "blue"));
mammals.Add(new HumanFemale("Krysten", 72, 15, "White", "Blue"));
mammals.Add(new Cat("Larry", 7, 2, "Orange", "Hazel"));
mammals.Add(new Dog("Walt", 3, 5, "Brown", "Hazel"));
mammals.Add(new HumanMale("Ryan", 72, 31, "White", "Blue"));
mammals.Add(new Cat("Blacky", 5, 10, "Black", "Brown"));
foreach (Mammal m in mammals)
{
m.Speak();
m.Walk();
}
Console.ReadKey();
}
} // end of Program class declaration
public abstract class Mammal
{
public Mammal(string name, int heightInInches, int age, string color, string eyeColor)
{
this.Name = name;
this.HeightInInches = heightInInches;
this.Age = age;
this.Color = color;
this.EyeColor = eyeColor;
}
private int _HeightInInches;
private int _Age;
/// <summary>
/// Gets or sets the age (in years).
/// </summary>
public int Age
{
get
{
return _Age;
}
set
{
if (value < 0)
{
throw new Exception("Invalid Age!");
}
_Age = value;
}
}
/// <summary>
/// Gets or sets the height.
/// </summary>
public int HeightInInches
{
get
{
return _HeightInInches;
}
set
{
if (value < 0)
{
throw new Exception("Invalid height!");
}
_HeightInInches = value;
}
}
/// <summary>
/// Gets or sets the name.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets the skin or fur color.
/// </summary>
public string Color { get; set; }
/// <summary>
/// Gets or sets the eye color.
/// </summary>
public string EyeColor { get; set; }
/// <summary>
/// Causes the mammal to speak.
/// </summary>
public virtual void Speak()
{
Console.WriteLine("The mammal spoke.");
}
/// <summary>
/// Causes the mammal to walk.
/// </summary>
public virtual void Walk()
{
Console.WriteLine("The mammal is walking.");
}
} // end of Mammal class declaration
public class Dog : Mammal
{
public Dog(string name, int heightInInches, int age, string color, string eyeColor)
: base (name, heightInInches, age, color, eyeColor)
{
}
public override void Speak()
{
Console.WriteLine("{0} the Dog says: 'Ruff!'", this.Name);
}
public override void Walk()
{
if (this.Age == 0)
{
Console.WriteLine("{0}, the {1} newborn puppy was just born and is too little to walk yet!", this.Name, this.Color);
}
else if (Age > 0 && Age <= 5)
{
Console.WriteLine("{0}, the {1} puppy is {2} years old and is running around like a hyper puppy, chewing up all the furniture. Oh nooo!", this.Name, this.Color, this.Age);
}
else if (Age > 5 && Age <= 10)
{
Console.WriteLine("{0}, the {1} dog is {2} years old and walks conservatively", this.Name, this.Color, this.Age);
}
else if (Age > 10)
{
Console.WriteLine("{0}, the {1} dog is {2} years old and walks very slowly, and has arthiritus in the joints.", this.Name, this.Color, this.Age);
}
}
} // end of Dog class
public class Cat : Mammal
{
public Cat(string name, int heightInInches, int age, string color, string eyeColor)
: base(name, heightInInches, age, color, eyeColor)
{
}
public override void Speak()
{
Console.WriteLine("{0} the Cat says: 'Meow!'", this.Name);
}
public override void Walk()
{
if (this.Age == 0)
{
Console.WriteLine("{0}, the {1} newborn Kitten was just born and is too little to walk yet!", this.Name, this.Color);
}
else if (Age > 0 && Age <= 5)
{
Console.WriteLine("{0}, the {1} Kitten is {2} years old and is running around like a hyper kitten!", this.Name, this.Color, this.Age);
}
else if (Age > 5 && Age <= 10)
{
Console.WriteLine("{0}, the {1} cat is {2} years old and walks conservatively", this.Name, this.Color, this.Age);
}
else if (Age > 10)
{
Console.WriteLine("{0}, the {1} cat is {2} years old and walks very slowly", this.Name, this.Color, this.Age);
}
}
} // end of Cat class
public abstract class Human : Mammal
{
public Human(string name, int heightInInches, int age, string color, string eyeColor)
: base(name, heightInInches, age, color, eyeColor)
{
}
public override void Walk()
{
if (this.Age == 0)
{
Console.WriteLine("{0}, the {1} newborn baby was just born and is too little to walk yet!", this.Name, this.Color);
}
else if (Age > 0 && Age <= 2)
{
Console.WriteLine("{0}, the {1} toddler is {2} years old and is crawling around!", this.Name, this.Color, this.Age);
}
else if (Age > 2 && Age <= 5)
{
Console.WriteLine("{0}, the {1} kid is {2} years old and is walking around!", this.Name, this.Color, this.Age);
}
else if (Age > 5 && Age <= 12)
{
Console.WriteLine("{0}, the {1} kid is {2} years old and walks briskly", this.Name, this.Color, this.Age);
}
else if (Age > 12 && Age <= 19)
{
Console.WriteLine("{0}, the {1} audlt is {2} years old and walks briskly", this.Name, this.Color, this.Age);
}
else if (Age > 20 && Age <= 60)
{
Console.WriteLine("{0}, the {1} adult is {2} years old and walks conservatively", this.Name, this.Color, this.Age);
}
else if (Age > 60)
{
Console.WriteLine("{0}, the {1} old person is {2} years old and walks with a cane", this.Name, this.Color, this.Age);
}
}
public override void Speak()
{
Console.WriteLine("The human spoke");
}
} // end of Human class
public class HumanMale : Human
{
public HumanMale(string name, int heightInInches, int age, string color, string eyeColor)
: base(name, heightInInches, age, color, eyeColor)
{
}
public override void Speak()
{
if (this.Age == 0)
{
Console.WriteLine("{0}, the newborn baby boy was just born and is too young to speak", this.Name);
}
if (this.Age > 0 && this.Age <= 3)
{
Console.WriteLine("{0}, the toddler boy babbles 'goo goo ga ga'", this.Name);
}
if (this.Age > 3 && this.Age <= 5)
{
Console.WriteLine("{0}, the toddler boy says, 'I like fire trucks'", this.Name);
}
if (this.Age > 5 && this.Age <= 12)
{
Console.WriteLine("{0}, the young boy says: I want to be a fireman'", this.Name);
}
if (this.Age > 12 && this.Age <= 20)
{
Console.WriteLine("{0}, the teenage boy says: I want a girlfriend'", this.Name);
}
if (this.Age > 20)
{
Console.WriteLine("{0}, the adult male says, 'Hey hey hey!'", this.Name);
}
}
} // end of HumanMale class
public class HumanFemale : Human
{
public HumanFemale(string name, int heightInInches, int age, string color, string eyeColor)
: base(name, heightInInches, age, color, eyeColor)
{
}
public override void Speak()
{
if (this.Age == 0)
{
Console.WriteLine("{0}, the newborn baby girl was just born and is too young to speak", this.Name);
}
if (this.Age > 0 && this.Age <= 3)
{
Console.WriteLine("{0}, the toddler girl babbles 'da da goo goo ga ga'", this.Name);
}
if (this.Age > 3 && this.Age <= 5)
{
Console.WriteLine("{0}, the girl says 'I wanna be a princess'", this.Name);
}
if (this.Age > 5 && this.Age <= 12)
{
Console.WriteLine("{0}, the young girl says: I AM a princess'", this.Name);
}
if (this.Age > 12 && this.Age <= 20)
{
Console.WriteLine("{0}, the teenage girl says: Like, totally. Did you see that other chick? Like, what was she wearing'", this.Name);
}
if (this.Age > 20)
{
Console.WriteLine("{0}, the adult female says, 'Yep, I'm a woman.'", this.Name);
}
}
} // end of HumanFemale class
} // end of namespace declaration