Hello!
Below I have a complete working program.with some simple classes one of
these is a generic class.
Now If I want to implement functionallity so I can compare animal with each
other or with other animal
for example compare a Chicken with a Cow in some way or a Cow with another
Cow
How is that done?
My attempted strategy is to set a constraint on T in such a way that T is
required to implement the
CompareTo method that exist in IEnumerable<T>
So I change the class definition header for the generic class from this
definition
public class Farm<T: IEnumerable<Twh ere T : Animal
to this
public class Farm<T: IEnumerable<Twh ere T : Animal, IEnumerable<T>
I then let Cow, Chicken and SuperCow inherit from IEnumarable
and then implement CompareTo in each class which is Cow, Chicken and
SuperCow
I tried this but run into several problems so my strategy was perhaps
complete
wrong.
So how is the correct way of doing this?
using System;
using System.Collecti ons.Generic;
using System.Collecti ons;
using System.Text;
namespace ConsoleApplicat ion15
{
class Program
{
static void Main(string[] args)
{
Farm<Animalfarm = new Farm<Animal>();
farm.Animal.Add (new Cow("Jack"));
farm.Animal.Add (new Chicken("Vera") );
farm.Animal.Add (new Chicken("Sally" ));
farm.Animal.Add (new SuperCow("Kevin "));
farm.MakeNoises ();
Farm<CowdairyFa rm = farm.GetCows();
dairyFarm.FeedT heAnimals();
foreach (Cow cow in dairyFarm)
if (cow is SuperCow)
((SuperCow)cow) .Fly();
Farm<AnimalnewF arm = farm + dairyFarm;
Console.ReadKey ();
}
}
public abstract class Animal
{
string name;
public Animal(string name)
{ this.name = name; }
public string Name
{ get { return name; } }
public abstract void MakeANoise();
public abstract void Feed();
}
public class Chicken : Animal
{
public Chicken(string name) : base(name) {}
public override void MakeANoise()
{ Console.WriteLi ne("{0} says 'cluck!'", Name); }
public override void Feed()
{ Console.WriteLi ne("{0} has been feed(Chicken)", Name); }
}
public class Cow : Animal
{
public Cow(string name) : base(name) {}
public override void MakeANoise()
{ Console.WriteLi ne("{0} says 'moo!'", Name); }
public override void Feed()
{ Console.WriteLi ne("{0} has been feed(Cow)", Name); }
}
public class SuperCow : Cow
{
public SuperCow(string name) : base(name) {}
public void Fly()
{ Console.WriteLi ne("{0} is flying!", Name); }
public override void MakeANoise()
{ Console.WriteLi ne("{0} says 'here I come to save the day!'",
Name); }
public override void Feed()
{ Console.WriteLi ne("{0} has been feed(SuperCow)" , Name); }
}
public class Farm<T: IEnumerable<Twh ere T : Animal
{
private List<Tanimals = new List<T>();
public List<TAnimal
{ get { return animals; } }
public IEnumerator<TGe tEnumerator()
{ return animals.GetEnum erator(); }
IEnumerator IEnumerable.Get Enumerator()
{ return animals.GetEnum erator(); }
public void MakeNoises()
{
foreach (T animal in animals)
animal.MakeANoi se();
}
public void FeedTheAnimals( )
{
foreach (T animal in animals)
animal.Feed();
}
public Farm<CowGetCows ()
{
Farm<CowcowFarm = new Farm<Cow>();
foreach (T animal in animals)
if (animal is Cow)
cowFarm.animals .Add(animal as Cow);
return cowFarm;
}
public static Farm<Toperator +(Farm<Tfarm1, Farm<Tfarm2)
{
Farm<Tresult = new Farm<T>();
foreach (T animal in farm1)
result.Animal.A dd(animal);
foreach (T animal in farm2)
if (!result.Animal .Contains(anima l))
result.Animal.A dd(animal);
return result;
}
public static implicit operator Farm<Animal>(Fa rm<Tfarm)
{
Farm<Animalresu lt = new Farm<Animal>();
foreach(T animal in farm)
result.Animal.A dd(animal);
return result;
}
}
}
//Tony
Below I have a complete working program.with some simple classes one of
these is a generic class.
Now If I want to implement functionallity so I can compare animal with each
other or with other animal
for example compare a Chicken with a Cow in some way or a Cow with another
Cow
How is that done?
My attempted strategy is to set a constraint on T in such a way that T is
required to implement the
CompareTo method that exist in IEnumerable<T>
So I change the class definition header for the generic class from this
definition
public class Farm<T: IEnumerable<Twh ere T : Animal
to this
public class Farm<T: IEnumerable<Twh ere T : Animal, IEnumerable<T>
I then let Cow, Chicken and SuperCow inherit from IEnumarable
and then implement CompareTo in each class which is Cow, Chicken and
SuperCow
I tried this but run into several problems so my strategy was perhaps
complete
wrong.
So how is the correct way of doing this?
using System;
using System.Collecti ons.Generic;
using System.Collecti ons;
using System.Text;
namespace ConsoleApplicat ion15
{
class Program
{
static void Main(string[] args)
{
Farm<Animalfarm = new Farm<Animal>();
farm.Animal.Add (new Cow("Jack"));
farm.Animal.Add (new Chicken("Vera") );
farm.Animal.Add (new Chicken("Sally" ));
farm.Animal.Add (new SuperCow("Kevin "));
farm.MakeNoises ();
Farm<CowdairyFa rm = farm.GetCows();
dairyFarm.FeedT heAnimals();
foreach (Cow cow in dairyFarm)
if (cow is SuperCow)
((SuperCow)cow) .Fly();
Farm<AnimalnewF arm = farm + dairyFarm;
Console.ReadKey ();
}
}
public abstract class Animal
{
string name;
public Animal(string name)
{ this.name = name; }
public string Name
{ get { return name; } }
public abstract void MakeANoise();
public abstract void Feed();
}
public class Chicken : Animal
{
public Chicken(string name) : base(name) {}
public override void MakeANoise()
{ Console.WriteLi ne("{0} says 'cluck!'", Name); }
public override void Feed()
{ Console.WriteLi ne("{0} has been feed(Chicken)", Name); }
}
public class Cow : Animal
{
public Cow(string name) : base(name) {}
public override void MakeANoise()
{ Console.WriteLi ne("{0} says 'moo!'", Name); }
public override void Feed()
{ Console.WriteLi ne("{0} has been feed(Cow)", Name); }
}
public class SuperCow : Cow
{
public SuperCow(string name) : base(name) {}
public void Fly()
{ Console.WriteLi ne("{0} is flying!", Name); }
public override void MakeANoise()
{ Console.WriteLi ne("{0} says 'here I come to save the day!'",
Name); }
public override void Feed()
{ Console.WriteLi ne("{0} has been feed(SuperCow)" , Name); }
}
public class Farm<T: IEnumerable<Twh ere T : Animal
{
private List<Tanimals = new List<T>();
public List<TAnimal
{ get { return animals; } }
public IEnumerator<TGe tEnumerator()
{ return animals.GetEnum erator(); }
IEnumerator IEnumerable.Get Enumerator()
{ return animals.GetEnum erator(); }
public void MakeNoises()
{
foreach (T animal in animals)
animal.MakeANoi se();
}
public void FeedTheAnimals( )
{
foreach (T animal in animals)
animal.Feed();
}
public Farm<CowGetCows ()
{
Farm<CowcowFarm = new Farm<Cow>();
foreach (T animal in animals)
if (animal is Cow)
cowFarm.animals .Add(animal as Cow);
return cowFarm;
}
public static Farm<Toperator +(Farm<Tfarm1, Farm<Tfarm2)
{
Farm<Tresult = new Farm<T>();
foreach (T animal in farm1)
result.Animal.A dd(animal);
foreach (T animal in farm2)
if (!result.Animal .Contains(anima l))
result.Animal.A dd(animal);
return result;
}
public static implicit operator Farm<Animal>(Fa rm<Tfarm)
{
Farm<Animalresu lt = new Farm<Animal>();
foreach(T animal in farm)
result.Animal.A dd(animal);
return result;
}
}
}
//Tony
Comment