// Make an array of Shapes.
Shape[] s = { new Hexagon(), new Circle(), new Triangle("Joe"),
new Circle("JoJo")} ;
for(int i = 0; i < s.Length; i++)
{
// Recall the Shape base class defines an abstract Draw()
// member, so all shapes know how to draw themselves.
s[i].Draw();
// Who's pointy?
if(s[i] is IPointy)
Console.WriteLine("-> Points: {0}", ((IPointy)s[i]).Points);
else
Console.WriteLine("-> {0}\'s not pointy!", s[i].PetName);
Console.WriteLine();
}
Console.ReadLine();
}
The output follows in Figure 9-3.
Figure 9-3.
Dynamically determining implemented interfaces
Interfaces As Parameters
Given that interfaces are valid .NET types, you may construct methods that take interfaces as
parameters as illustrated by the CloneMe() method earlier in this chapter. For the current example,
assume you have defined another interface named IDraw3D:
Do'stlaringiz bilan baham: |