list and two classes

Apr 7 2019 5:29 PM
public class A
{
public int x;
public int y;
public A(int x, int y)
{
this.x = x;
this.y = y;
}
public int Make()
{
return 1;
}
}
public class B : A
{
public int z;
public B(int x, int y, int z) : base(x, y)
{
this.z = z;
}
public int Make()
{
return 1;
}
}
class Program
{
static void Main(string[] args)
{
List<A> myA = new List<A>();
myA.Add(new A(1, 2));
myA.Add(new B(3, 4, 5));
Console.WriteLine(myA[0].Make());
Console.WriteLine(((B)myA[1]).Make());
for(int i = 0; i < 2; i++)
{
Console.WriteLine(myA[i].Make()); //how to send classB.make this element.
}

Answers (1)