开发者

How do you make a nested class that can access the members of the class that created it?

Im programming in C#.NET. I want to create a nested class that can access members of the instance that created it but I can't seem to figure out how.

This is what I want to do:

Car x = new Car()
x.color = "red";
x.Door frontDoor = new x.Door();
MessageBox.Show开发者_如何学Go(frontDoor.GetColor());    // So I want the method GetColor of the class Front Door to be able to access the color field/property of the Car instance that created it.

How do I do it? I tried nesting the Door class inside the Car class but it can't access the members of the Car class that way. Do I need to make Car inherit the door class or something?


The easiest way is to give the Door type a reference back to the Car which created it.

For example:

class Car { 
  public string color;
  public Door Door() { return new Door(this); }
  class Door { 
    Car owner;
    Door(Car owner) { this.owner = owner; }
    string GetColor() { return owner.color; }
  }
}


This is not generally something you would want to do in a well designed object model.

The DOOR is a component of the CAR object, but should not have a dependency on the CAR object. You should pass the color of the CAR into the DOOR either through the constructor or via setter Property of the DOOR.


I dont think the door should have any reference to the car at all. The relationship should be model as an association - "owns a" relationship. The car should owns the car door, and even responsible for creating the doors when the car is created. The constructor of the door should take in a parameter to say what colour of door to create e.g door = new Door(this.Colour); whereby 'this' is the reference to the current car object. If you think about it logically, I can change the car colour to blue and my door colour will be changed along with that. However, if I decided to have the door(s) colour to red, it is not necessary that the car colour has to change.


This really is not a nested class, but rather an object of type Door referenced by a property on an object of typeCar. As references are inherently one-way in C# (and most other object oriented languages), you will need to add a reference in the other direction as well, in order to be able to do what are are trying to.

Here is one possible implementation: Add a constructor to Door, accepting a reference to a Car as parameter. The constructor should save the reference in a private readonly field. In the GetColor() method, use the reference to get the color of the Car instance.

Then, consider replacing the GetColor() with a get-only Car property on Door. Depending on the relationship in your real classes (I assume you are not really modelling cars), this might or might not be preferable.


If you want to be able to access the private members of Car in the door class you need to use inheritance, if you just want to access the public members of Car inside the Door class you can use Composition

Like this:

public class Car
    {
        public string Color;
    }

    public class Door
    {
        public string T;

        public Car C;
    }

Although this is not really a logical approach. It should be that there is a Door property inside the Car class.


I'd make the Door class take a Func<Color> in its constructor and the Car class can pass it's GetColor() method through that:

public class Car
{
    private class Door
    {
        public Door(Func<Color> getColourFunc)
        {
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜