I am trying to instantiate objects for an array of 10 athletes of the "Sport" abstract class which takes in name and age for each athlete, then there is two derived classes one for "Tennis" athletes and one for "Golf" athletes.
class Program
{
static void Main(string[] args)
{
Sport[] athlete = new Sport[10];
athlete[0] = new Tennis("John Smith", 18, "Tennis", 5.0, 92);
athlete[1] = new Tennis("Lisa Townsend", 15, "Tennis");
athlete[2] = new Tennis("Brian Mills", 17, "Tennis", 4.0, 83);
athlete[3] = new Golf("Stacey Bell", 16, "Golf", 10, 20);
athlete[4] = new Golf("Tom Spehr", 18, "Golf", 9, 12);
athlete[5] = new Golf("Sam Calen", 14, "Golf");
athlete[6] = new Tennis("Karen Strong", 17, "Tennis", 3.0, 78);
athlete[7] = new Golf("Ken Able", 15, "Golf", 15, 16);
athlete[8] = new Tennis("Troy Soni", 18, "Tennis", 4.5, 93);
athlete[9] = new Golf("Toni Palmer", 17, "Golf", 8, 22);
for (int i = 0; i < 10; i++)
{
Console.WriteLine("{0}", athlete[i]);
}
}
}
I am trying to print the array like this but it is not outputting properly. Also when I try to print the data fields individually as
Console.WriteLine("{0} {1}", athlete[i].name, athlete[i].age)
I can get it to output each athletes name and age but if I try to add the other fields they will not output. Should I be declaring each array object as "Sport" rather than Tennis or Golf?
Edit:
Here is the Sport class
abstract class Sport
{
protected string name;
protected int age;
public Sport(string name, int age)
{
Name = name;
Age = age;
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public abstract void Performance();
}
and here is the derived Tennis class (the golf class is structured the same way just slight changes to the variable names)
class Tennis : Sport
{
private string type;
private double rating;
private int serveSpeed;
public Tennis(string name, int age, string type, double rating, int serveSpeed) : base(name, age)
{
Rating = rating;
Type = type;
ServeSpeed = serveSpeed;
}
public Tennis(string name, int age, string type) : base(name, age)
{
}
public double Rating
{
get
{
return rating;
}
set
{
rating = value;
}
}
public string Type
{
get
{
return type;
}
set
{
type = "Tennis";
}
}
public int ServeSpeed
{
get
{
return serveSpeed;
}
set
{
serveSpeed = value;
}
}