Generics lond discussion simple code
I wrote the following simple class below to clear up some points.
The class below that is from a book, and I am using it to try and make the code clear to meâ¦
Can people please comment on what I am saying, I am sure a lot of it wont be accurate.
My comments:
The class below has a defualt and non default construct used to create instances of the class student.. Without these an object of this type cant be contructed or instaniated.
When this line is called
blank = new student();
a blank template is created for the student class. Putting a ââ into the name veriable and zeror into the idâ¦
when either property such as myStudent.Name it uses the get to return the valueâ¦
Now moving onto the Generic class Farm<T>
Where is the defualt construct ?
private List<T> animals = new List<T>();
this apppears close to it and it appear to me that the property
public List<T> Animals
{
get
{
return animals;
}
}
Animals is being used like a handle to set up the animals collection
Eg..
farm.Animals.Add(new Cow("Jack"));
Animals uses the get to return animals the actual collection for storing the collection of animals
If we substitute animals for Animals we are adding via animals.Add to this collection ?
Initialy to me, it seems the get should be be a set as this seems to be setting values ?
I see this simulary to mystudent.Name which is a get to access name, Via Name.
These lines are confusing
public IEnumerator<T> GetEnumerator()
{
return animals.GetEnumerator();
}
I can see itâs a method, and what it returns, there is no yield and its not implemented as in the usual wayâ¦
Also the line return animals.GetEnumerator(); The book says you simply return the enumerator returned by Animalsâ¦. ?
But Farm<T> : IEnumerable<T> inherits from this inteface not Animals ?
animals is just a type, that contains the collection of animals, I can see you are trying to iterate through it in the foreach loop, but where is it implemented..
should it be (obviously not it wont work) but Farm<T>.Animals.GetEnumerator()
what is this a method a it looks odd the way its defined plus where is it implemented..
thanks Laurence
Also I am aware ArrayList() implements GetEnumerator() as it uses a foreach command.
IEnumerator IEnumerable.GetEnumerator()
{
return animals.GetEnumerator();
}
using System;
using System.Collections.Generic;
using System.Text;
namespace stud
{
public class student
{
private string name;
private int Id;
public student()
{
// default contructor.
}
public student(string newname, int newId)
{
name = newname;
Id = newId;
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int ID
{
get
{
return Id;
}
set
{
Id = value;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace stud
{
class Program
{
static void Main(string[] args)
{
student blank = new student();
student myStudent = new student("Frank", 1);
Console.WriteLine("{0} is students name {1} is students ID", myStudent.Name, myStudent.ID);
Console.WriteLine(myStudent.ToString());
Console.WriteLine("{0} is students name {1} is students ID", blank.Name, blank.ID);
Console.WriteLine(blank.ToString());
Console.ReadKey();
}
}
}
Generic Class is confusing
using System;
using System.Collections.Generic;
using System.Text;
namespace ch12ex02
{
public abstract class Animal
{
protected string name;
public String Name
{
get
{
return name;
}
set
{
name = value;
}
}
public Animal()
{
// default constructor
name = "The animal with no name ";
}
public Animal(string newName)
{
// non defualt contructor.
name = newName;
}
public void Feed()
{
Console.WriteLine("{0} has been fed.", name);
}
public abstract void MakeNoise();
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ch12ex02
{
public class chicken : Animal
{
public void LayEgg()
{
Console.WriteLine("{0} has laid an egg", name);
}
public chicken(string newName): base(newName)
{
}
public override void MakeNoise()
{
Console.WriteLine("{0} says Cluck", name);
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ch12ex02
{
public class Cow: Animal
{
public void Milk()
{
Console.WriteLine("{0} has been milked.", name);
}
public Cow ( string newName ): base(newName)
{
//This willl call the non defualt constructor of the base animal class.
}
public override void MakeNoise()
{
Console.WriteLine("{0} says moo!", name);
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace ch12ex02
{
public class Farm<T> : IEnumerable<T>
where T : Animal
{
private List<T> animals = new List<T>();
public List<T> Animals
{
get
{
return animals;
}
}
public IEnumerator<T> GetEnumerator()
{
return animals.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return animals.GetEnumerator();
}
public void MakeNoises()
{
foreach (T animal in animals)
{
animal.MakeNoise();
}
}
public void FeedTheAnimals()
{
foreach(T animal in animals)
{
animal.Feed();
}
}
public Farm<Cow> GetCows()
{
Farm<Cow> cowFarm = new Farm<Cow>() ;
foreach (T animal in animals)
{
if (animal is Cow)
{
cowFarm.Animals.Add(animal as Cow);
}
}
return cowFarm;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ch12ex02
{
public class SuperCow: Cow
{
public void fly()
{
Console.WriteLine("{0} is flying", name);
}
public SuperCow(string newName)
: base(newName)
{
}
public override void MakeNoise()
{
Console.WriteLine("{0} says here i come to save the day !", name);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ch12ex02
{
class Program
{
static void Main(string[] args)
{
Farm<Animal> farm = new Farm<Animal>();
farm.Animals.Add(new Cow("Jack"));
farm.Animals.Add(new chicken("vera"));
farm.Animals.Add(new chicken("Sally Ally"));
farm.Animals.Add(new SuperCow ("Kevin"));
farm.MakeNoises();
Farm<Cow> dairyFarm = farm.GetCows();
dairyFarm.FeedTheAnimals();
foreach (Cow cow in dairyFarm)
{
if (cow is SuperCow)
{
(cow as SuperCow).fly();
}
}
Console.ReadKey();
}
}
}
|