My brother wrote this code for me to show an example of polymorhism. But I don’t really understand how it all works, starting with the parameters. Can someone dissect the code and tell me a little bit about how it all works together and how it functions?
EDIT: Here’s a specific question: mammals.Add(new Cat(“Snow white”, 1, 0, “White”, “blue”)); What do the numbers do? What is their purpose and how do they work with the code?
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
For the specific question in the EDIT:
The line can be rewritten as:
In the first line you are calling the constructor of Cat:
passing “Snow white” as name, 1 as heightInInches, 0 as age, “White” as color and “blue” as eyeColor. The constructor is empty (it does nothing, but it is based upon the Mammal constructor (this is the meaning of
base(name, heightInInches, age, color, eyeColor)), which is called passing the same parameters. This means thatpublic Mammal(string name, int heightInInches, int age, string color, string eyeColor)is executed, so you end up with an instance of Cat where all the properties have the values that were passed as parameters.Now you execute
Here you are adding the instance of Cat to a list of Mammal. You ca do that because Add expect a Mammal as argument, and aCat is a Mammal, since its class is derived from Mammal.