【问题标题】:How to access to the parent object in c#c#中如何访问父对象
【发布时间】:2010-12-28 18:00:49
【问题描述】:

我有一个“米”课。 “meter”的一个属性是另一个称为“production”的类。 我需要通过 reference 从生产类访问仪表类(额定功率)的属性。在 Meter 实例化时,powerRating 是未知的。

我该怎么做?

提前致谢

public class Meter
{
   private int _powerRating = 0; 
   private Production _production;

   public Meter()
   {
      _production = new Production();
   }
}

【问题讨论】:

标签: c# oop parent


【解决方案1】:

将仪表实例的引用存储为生产中的成员:

public class Production {
  //The other members, properties etc...
  private Meter m;

  Production(Meter m) {
    this.m = m;
  }
}

然后在 Meter 类中:

public class Meter
{
   private int _powerRating = 0; 
   private Production _production;

   public Meter()
   {
      _production = new Production(this);
   }
}

另请注意,您需要实现访问器方法/属性,以便 Production 类可以实际访问 Meter 类的 powerRating 成员。

【讨论】:

  • 如果这两个类在不同的程序集中,并且包含仪表的程序集有一个VS2010项目引用到包含生产程序的程序集怎么办?
  • @Snowy:由于循环引用是不可能的,您需要重新安排您的实现并将一个类移动到另一个程序集(或复制该类,如果没有其他帮助的话)。
【解决方案2】:

我不会直接在子对象中引用父对象。在我看来,孩子不应该知道父母的任何事情。这将限制灵活性!

我会用事件/处理程序来解决这个问题。

public class Meter
{
    private int _powerRating = 0;
    private Production _production;

    public Meter()
    {
        _production = new Production();
        _production.OnRequestPowerRating += new Func<int>(delegate { return _powerRating; });
        _production.DoSomething();
    }
}

public class Production
{
    protected int RequestPowerRating()
    {
        if (OnRequestPowerRating == null)
            throw new Exception("OnRequestPowerRating handler is not assigned");

        return OnRequestPowerRating();
    }

    public void DoSomething()
    {
        int powerRating = RequestPowerRating();
        Debug.WriteLine("The parents powerrating is :" + powerRating);

    }

    public Func<int> OnRequestPowerRating;
}

在这种情况下,我使用 Func 泛型解决了它,但可以使用“普通”函数来完成。 这就是为什么孩子(Production)完全独立于它的父母(Meter)。


但是!如果事件/处理程序太多,或者您只想传递父对象,我会用接口解决它:

public interface IMeter
{
    int PowerRating { get; }
}

public class Meter : IMeter
{
    private int _powerRating = 0;
    private Production _production;

    public Meter()
    {
        _production = new Production(this);
        _production.DoSomething();
    }

    public int PowerRating { get { return _powerRating; } }
}

public class Production
{
    private IMeter _meter;

    public Production(IMeter meter)
    {
        _meter = meter;
    }

    public void DoSomething()
    {
        Debug.WriteLine("The parents powerrating is :" + _meter.PowerRating);
    }
}

这看起来与解决方案提到的几乎相同,但接口可以在另一个程序集中定义,并且可以由多个类实现。


问候, 杰伦·范·朗根。

【讨论】:

    【解决方案3】:

    您需要向 Production 类添加一个属性并将其设置为指向其父级,默认情况下不存在。

    【讨论】:

      【解决方案4】:

      为什么不更改Production 上的构造函数,让您在构造时传入引用:

      public class Meter
      {
         private int _powerRating = 0; 
         private Production _production;
      
         public Meter()
         {
            _production = new Production(this);
         }
      }
      

      Production 构造函数中,您可以将其分配给私有字段或属性。那么Production 将始终可以访问父级。

      【讨论】:

        【解决方案5】:

        您可以向您的 Production 对象添加一个名为“SetPowerRating(int)”的方法,该方法在 Production 中设置一个属性,并在使用 Production 对象中的属性之前在您的 Meter 对象中调用它?

        【讨论】:

        • 我认为这违反了KISS;必须记住在调用属性之前调用函数会使 IMO 过于复杂。
        • 没错,上面的设计听起来好像需要重新思考。我刚刚提供了一个解决方案,它可能不是最好的。
        【解决方案6】:

        我会给父母一个 ID,并将 parentID 存储在子对象中,这样您就可以根据需要提取有关父母的信息,而无需创建 parent-owns-child/child-owns-parent 循环。

        【讨论】:

          【解决方案7】:

          类似这样的:

            public int PowerRating
              {
                 get { return base.PowerRating; } // if power inherits from meter...
              }
          

          【讨论】:

          • 这是父子关系,而不是“继承”关系。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-05-08
          • 2013-07-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-24
          • 1970-01-01
          相关资源
          最近更新 更多