BrickInTheWall
07-26-2009, 09:01 PM
Hi everybody, I need some help clearing some things up. The following code is a bit lengthy for a post but very simple...
I have 3 classes: Animal, Cow and Chicken (which are derived from the abstract class Animal).
Animal.cs:
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);
}
}
Cow.cs:
public class Cow : Animal
{
public void Milk()
{
Console.WriteLine("{0} has been milked.", name);
}
public Cow(string newName) : base(newName)
{
}
}
Chicken.cs:
public class Chicken : Animal
{
public void LayEgg()
{
Console.WriteLine("{0} has laid an egg.", name);
}
public Chicken(string newName) : base(newName)
{
}
}
and here the main project file:
program.cs:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ch11Ex01
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Create an Array type collection of Animal objects and use it.");
Animal[] animalArray = new Animal[2];
Cow myCow = new Cow("Deirdre");
Chicken myChicken = new Chicken("Ken");
animalArray[0] = myCow;
animalArray[1] = myChicken;
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}.", myCow.Name, animalArrayList.IndexOf(myCow));
myCow.Name = "Janice";
Console.WriteLine("The animal is now called {0}.", ((Animal)animalArrayList[1]).Name);
Console.ReadKey();
}
}
}
What I mainly don't understand is the following:
1) The array Animal[] animalArray = new Animal[2]; contains objects created by Animals derived classes...how is the possible? This seems a bit like when you have pointers to the base class in an array in C++ (not really I know but thats what it reminds me of) and I don't understand why this is possible...let alone creating an array of a class type that is abstract.
2) The type conversions in some lines of the code are irritating me as I don't understand the full reason as to why this conversion is necessary. Like here:
((Chicken)animalArray[1]).LayEgg();
Any help on these two question I would really appreciate!
Cheers,
Chris
I have 3 classes: Animal, Cow and Chicken (which are derived from the abstract class Animal).
Animal.cs:
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);
}
}
Cow.cs:
public class Cow : Animal
{
public void Milk()
{
Console.WriteLine("{0} has been milked.", name);
}
public Cow(string newName) : base(newName)
{
}
}
Chicken.cs:
public class Chicken : Animal
{
public void LayEgg()
{
Console.WriteLine("{0} has laid an egg.", name);
}
public Chicken(string newName) : base(newName)
{
}
}
and here the main project file:
program.cs:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ch11Ex01
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Create an Array type collection of Animal objects and use it.");
Animal[] animalArray = new Animal[2];
Cow myCow = new Cow("Deirdre");
Chicken myChicken = new Chicken("Ken");
animalArray[0] = myCow;
animalArray[1] = myChicken;
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}.", myCow.Name, animalArrayList.IndexOf(myCow));
myCow.Name = "Janice";
Console.WriteLine("The animal is now called {0}.", ((Animal)animalArrayList[1]).Name);
Console.ReadKey();
}
}
}
What I mainly don't understand is the following:
1) The array Animal[] animalArray = new Animal[2]; contains objects created by Animals derived classes...how is the possible? This seems a bit like when you have pointers to the base class in an array in C++ (not really I know but thats what it reminds me of) and I don't understand why this is possible...let alone creating an array of a class type that is abstract.
2) The type conversions in some lines of the code are irritating me as I don't understand the full reason as to why this conversion is necessary. Like here:
((Chicken)animalArray[1]).LayEgg();
Any help on these two question I would really appreciate!
Cheers,
Chris