C#属性的循环引用 [英] Circular Reference On C# Properties

查看:134
本文介绍了C#属性的循环引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

可能这是一个新手问题,但是在这里.

我正在开发纸牌游戏,并且有一个名为Player的类.

Player具有一个名为Partner的属性(另一个Player)和另一个名为Points的属性.

Hi Guys ,

May be this is a newbie question but here it is.

I am developing a card game and I have a class named Player.

Player has a property called Partner ( another Player ) and another property called Points.

public class Player
{  
  public Player()
  {
  }
  public Player Partner { get; set; }
  private int points = 0;
  public int Points 
  { 
    get { return points;}
    set { if (Partner != null) Partner.Points = value;
          points = value; 
        }                             // -> Here is the problem cause p1.Partner = p2 and p2.Partner = p1
}


问题是:如何为"player"设置Points属性并自动设置其伙伴" Points属性的最优雅方式"?

例如:


The question is : How is the most "elegant way" to set the Points property for a player and have its Partner Points property set automatically ?

For instance:

Player p1 = new Player();
Player p2 = new Player();
p1.Partner = p2;
p2.Partner = p1;
p1.Points = 10;
int p = p2.Points



p应该等于10.


预先感谢,
马塞洛
巴西<



p should be equal to 10.


Thanks in advance ,
Marcelo
Brazil<

推荐答案

我会保留两个球员都使用的points对象.

I would maintain a points object that is used by both players

public class Points
{
    public int Value { get; set; }
    public Points() {}
}

public class Player
{
    public Points PointsValue { get; set; }
    public Player Partner { get; set; }

    public Player(Points points)
    {
        this.PointsValue = points;
        if (Partner != null)
        { 
            Partner.PointsValue = points;
        }
    }

    public Player(Player partner)
    {
        Partner = partner;
        PointsValue = Partner.PointsValue;
    }
}



我是在上面编写代码的,但是您应该明白这一点.当点值更改时,两个玩家都可以访问新值.



I did the code above off the top of my head, but you should get the idea. When the point value changes, BOTH players will have access to the new value.


似乎您需要实现Observer设计模式:
http://www.dofactory.com/Patterns/PatternObserver.aspx [
It seems that you need to implement Observer design pattern :
http://www.dofactory.com/Patterns/PatternObserver.aspx[^]

So you can implement it in this way :

    public class PlayersObserver
    {
        public List<player> Players =new List<player>();

        private int _points;
        public int Points
        {
            get { return _points; }
            set
            {
                _points = value;
                foreach (var player in Players)
                {
                    player.Points = value;
                }
            }
        }
    }
    
    
    public class Player
    {
        public Player()
        {
        }
        public Player Partner { get; set; }

        public int Points { get; set; }
    }
</player></player>




它的用法是:




And it usage is :

Player p1 = new Player();
Player p2 = new Player();
PlayersObserver playersObserver = new PlayersObserver();
playersObserver.Players.Add(p1);
playersObserver.Players.Add(p2);

playersObserver.Points = 10;

int p = p2.Points;



此时p的值为10.

这样,您将对观察者进行任何操作,并将其应用于列表中的那些对象,这样您就不仅限于两个合作伙伴了.

希望它能对您有所帮助.



p will have the value of 10 at this point.

By this way you will do whatever you want with observer and it will apply that to those objects in its List so you are not confined only to two partners.

Hope it helps.


尽管我更喜欢JSOP的答案,您可以在不需要额外的类的情况下做到这一点,但是感觉有点像黑客一样目前尚不清楚发生了什么:


Although I prefer JSOP''s answer, you can do it without the extra class, but it feels a bit more of a hack as it is less clear what is happening:


public int Points
{
   get { return points;}
   set 
   { 
      if(points == value)
         return; //This will stop a infinitely recurive set, 
                 //but not suitable if multi-threaded access is needed.
      points = value;
      if (Partner != null) 
         Partner.Points = value;
   }
}



只是另一种选择,但正如我所说的,优先选择JSOP.



Just another option, but as I say go for JSOP''s in preference.


这篇关于C#属性的循环引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆