Free mag vol1 | Page 353

CHAPTER 8  WORKING WITH INTERFACES The output is as follows: ***** Fun with Interfaces ***** Drawing NoName the Hexagon -> Points: 6 Drawing NoName the Circle -> NoName's not pointy! Drawing Joe the Triangle -> Points: 3 Drawing JoJo the Circle -> JoJo's not pointy! 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: // Models the ability to render a type in stunning 3D. public interface IDraw3D { void Draw3D(); } Next, assume that two of your three shapes (ThreeDCircle and Hexagon) have been configured to support this new behavior: // Circle supports IDraw3D. class ThreeDCircle : Circle, IDraw3D { ... public void Draw3D() { Console.WriteLine("Drawing Circle in 3D!"); } } // Hexagon supports IPointy and IDraw3D. class Hexagon : Shape, IPointy, IDraw3D { ... public void Draw3D() { Console.WriteLine("Drawing Hexagon in 3D!"); } } Figure 8-3 presents the updated Visual Studio class diagram. 291