Protecting Nested Classes

You can nest classes inside other classes in C#, and nested classes can access members of the host class, even private members . Nested classes work much like any other class member when it comes to inheritance; for example, say that that the Window class has a nested Point class, which keeps track of the upper-left point of the window. We might make the Point class protected like this:

 
 public class Window {  private Point location;  public Window(int x, int y)   {  this.location = new Point(x, y);  }   public Window()   {  this.location = new Point(100, 100);  }   public virtual void Open()   {     System.Console.WriteLine("Opening window at [{0}, {1}]",     location.x, location.y);   }  protected class Point   {   public int x, y;   public Point(int x, int y)   {   this.x = x;   this.y = y;   }   }  } 

Now say that you derive the Menu class from Window ; because the nested Point class was declared protected , Menu can use that class also (if Point had been declared private , Menu couldn't use it):

 
  public class Menu : Window  {  private Point location;  public Menu(int x, int y)   {  location = new Point(x, y);  }   public override void Open()   {     System.Console.WriteLine("Opening menu at [{0}, {1}]",     location.x, location.y);   } } 

You can see all this at work in ch04_08.cs (as shown in Listing 4.8), where we create Window and Menu objects, both of which use the Point class. Point is a nested protected class in the Window class, so when you derive the Menu class from Window , Menu also has access to the Point class.

Listing 4.8 Using Nested Classes (ch04_08.cs)
 public class ch04_08 {   static void Main()   {  Window window = new Window(100, 200);   window.Open();   Menu menu = new Menu(100, 300);   menu.Open();  } } public class Window {   private Point location;   public Window(int x, int y)   {     this.location = new Point(x, y);   }   public Window()   {     this.location = new Point(100, 100);   }   public virtual void Open()   {     System.Console.WriteLine("Opening window at [{0}, {1}]",     location.x, location.y);   }   protected class Point   {     public int x, y;     public Point(int x, int y)     {       this.x = x;       this.y = y;     }   } } public class Menu : Window {   private Point location;   public Menu(int x, int y)   {     location = new Point(x, y);   }   public override void Open()   {     System.Console.WriteLine("Opening menu at [{0}, {1}]",     location.x, location.y);   } } 

Here's what you see when you run ch04_08.cs. Note that both the Window and Menu class can use the nested Point class:

 
 C:\>ch04_08 Opening window at [100, 200] Opening menu at [100, 300] 

It's often better for the sake of information hiding to make nested classes like Point private and inaccessible in derived classes. In this case, Menu wouldn't have direct access to the Point class, and you'd have to provide accessor methods like GetPoint and SetPoint to set the location of the upper-left side of the menu.



Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net