Inheritance and Polymorphism
Polymorphic Arrays
A base-class array can hold different derived objects and call virtual methods.
polymorphism
Polymorphism uses the derived method that matches the actual object.
Polymorphic Arrays
PolymorphicArrays.cs
Replay: real traced execution (multi-file project)
using System;
class Shape
{
public virtual string Name()
{
return "shape";
}
}
class Circle : Shape
{
public override string Name()
{
return "circle";
}
}
class Square : Shape
{
public override string Name()
{
return "square";
}
}
class Program
{
static void Main()
{
string separator = ",";
Shape[] shapes = { new Circle(), new Square() };
string summary = "";
foreach (Shape shape in shapes)
{
if (summary.Length > 0)
{
summary += separator;
}
summary += shape.Name();
}
Console.WriteLine($"separator={separator}");
Console.WriteLine($"summary={summary}");
}
}
using System;
class Shape
{
public virtual string Name()
{
return "shape";
}
}
class Circle : Shape
{
public override string Name()
{
return "circle";
}
}
class Square : Shape
{
public override string Name()
{
return "square";
}
}
class Program
{
static void Main()
{
string separator = " | ";
Shape[] shapes = { new Circle(), new Square() };
string summary = "";
foreach (Shape shape in shapes)
{
if (summary.Length > 0)
{
summary += separator;
}
summary += shape.Name();
}
Console.WriteLine($"separator={separator}");
Console.WriteLine($"summary={summary}");
}
}
using System;
class Shape
{
public virtual string Name()
{
return "shape";
}
}
class Circle : Shape
{
public override string Name()
{
return "circle";
}
}
class Square : Shape
{
public override string Name()
{
return "square";
}
}
class Program
{
static void Main()
{
string separator = "/";
Shape[] shapes = { new Circle(), new Square() };
string summary = "";
foreach (Shape shape in shapes)
{
if (summary.Length > 0)
{
summary += separator;
}
summary += shape.Name();
}
Console.WriteLine($"separator={separator}");
Console.WriteLine($"summary={summary}");
}
}
separator ← ,, shapes ← Shape[], summary ← (empty)
28{29 static void Main()30 {31 string separator→ , = ","; //@separator=" | ", "/"32 Shape[] shapes→ Shape[] = { new Circle(), new Square() };33 string summary→ (empty) = "";foreach (Shape shape in shapes)
pass 1 of 235foreach (Shape shapeCircle in shapesShape[])36{37 if (summary.Length > 0)38 {39 summary += separator;40 }4142 summary += shapeCircle.Name();43}summary ← circle
42 summary→ circle += shapeCircle.Name();43}foreach (Shape shape in shapes)
pass 2 of 235foreach (Shape shapeSquare in shapesShape[])36{37 if (summary.Length > 0)summary ← circle,
36{37 if (summary.Length6 > 0)38 {39 summary→ circle, += separator,;40 }summary += shape.Name();
42 summary += shapeSquare.Name();43}summary ← circle,square
42 summary→ circle,square += shapeSquare.Name();43}Console.WriteLine($"separator={separator}");
45 Console.WriteLine($"separator={separator,}");46 Console.WriteLine($"summary={summarycircle,square}");47}outputseparator=, summary=circle,square
separator ← | , shapes ← Shape[], summary ← (empty)
28{29 static void Main()30 {31 string separator→ | = " | ";32 Shape[] shapes→ Shape[] = { new Circle(), new Square() };33 string summary→ (empty) = "";foreach (Shape shape in shapes)
pass 1 of 235foreach (Shape shapeCircle in shapesShape[])36{37 if (summary.Length > 0)38 {39 summary += separator;40 }4142 summary += shapeCircle.Name();43}summary ← circle
42 summary→ circle += shapeCircle.Name();43}foreach (Shape shape in shapes)
pass 2 of 235foreach (Shape shapeSquare in shapesShape[])36{37 if (summary.Length > 0)summary ← circle |
36{37 if (summary.Length6 > 0)38 {39 summary→ circle | += separator | ;40 }summary += shape.Name();
42 summary += shapeSquare.Name();43}summary ← circle | square
42 summary→ circle | square += shapeSquare.Name();43}Console.WriteLine($"separator={separator}");
45 Console.WriteLine($"separator={separator | }");46 Console.WriteLine($"summary={summarycircle | square}");47}outputseparator= | summary=circle | square
separator ← /, shapes ← Shape[], summary ← (empty)
28{29 static void Main()30 {31 string separator→ / = "/";32 Shape[] shapes→ Shape[] = { new Circle(), new Square() };33 string summary→ (empty) = "";foreach (Shape shape in shapes)
pass 1 of 235foreach (Shape shapeCircle in shapesShape[])36{37 if (summary.Length > 0)38 {39 summary += separator;40 }4142 summary += shapeCircle.Name();43}summary ← circle
42 summary→ circle += shapeCircle.Name();43}foreach (Shape shape in shapes)
pass 2 of 235foreach (Shape shapeSquare in shapesShape[])36{37 if (summary.Length > 0)summary ← circle/
36{37 if (summary.Length6 > 0)38 {39 summary→ circle/ += separator/;40 }summary += shape.Name();
42 summary += shapeSquare.Name();43}summary ← circle/square
42 summary→ circle/square += shapeSquare.Name();43}Console.WriteLine($"separator={separator}");
45 Console.WriteLine($"separator={separator/}");46 Console.WriteLine($"summary={summarycircle/square}");47}outputseparator=/ summary=circle/square