简体   繁体   English

C#基类具有派生类成员,并且会循环

[英]C# base class have a derived class member and it loops

I have this class: 我有这个课:

public class GameData
{
    public int[,] mat { get; set; }
    public int dim { get; set; }
    public int goal { get; set; }
    public Game game { get; set; }

    public GameData()
    {
        game = new Game();
    }
}

And this two other derived classes: 还有另外两个派生类:

public class Game :GameData
{
    private Mover m;

    public Game()
    {
        dim = 4;
        goal = 16;
        mat = new int[dim, dim];
        m = new Mover();
    }
    /*Methods*/
}

public class Mover : GameData
{
    /*Methods*/
}

It loops because Game constructor calls GameData constructor and so on. 之所以循环,是因为Game构造函数调用GameData构造函数,依此类推。 How can I do? 我能怎么做? I am starting whit OOP programming and I am not sure this three classes are the best way to set the program. 我正在开始进行OOP编程,但是我不确定这三个类是设置程序的最佳方法。 GameData contains common datas, Game contains the method to play and Mover contains a group of method used by Game GameData包含公共数据, Game包含要玩的方法, Mover包含Game使用的一组方法

Honestly inheritance doesn't make any sense here. 老实说,继承在这里没有任何意义。 Inheritance is an "is a type of" relationship (in other words, is Game a type of GameData ? I doubt it. 继承是一种“是一种”关系(换句话说, GameGameData吗?我对此表示怀疑。

Instead use composition! 而是使用合成! Just have class hold the objects they care about. 只是让类持有他们关心的对象。 Composition is a "has a" relationship, which makes a lot more sense in your case. 合成是一种“具有”关系,这在您的情况下更为有意义。 Honestly, I would invert the Game / GameData relationship, a Game probaly holds GameData ; 老实说,我会颠倒Game / GameData关系, Game可能持有GameData not the other way around. 并非相反。 Only you know what you need though. 但是只有您知道您需要什么。

public class GameData
{
    public int[,] mat { get; set; }
    public int dim { get; set; }
    public int goal { get; set; }
    public Game game { get; set; }

    public GameData()
    {
        game = new Game();
        dim = 4;
        goal = 16;
        mat = new int[dim, dim];
    }
}

public class Game
{
    private Mover m;

    public Game()
    {
        m = new Mover();
    }
    /*Methods*/
}

public class Mover
{
    /*Methods*/
}

This sentence 这个句子

public GameData() { game = new Game(); public GameData(){game = new Game(); } }

makes no sense; 没有意义; as GameData is the parent, is not logical to have a child inside the parent. 由于GameData是父级,因此在父级中生一个孩子是不合逻辑的。 Obviously that makes a loop, as you are creating a Game : GameData over and over with that sentence. 显然,这会导致循环,因为您正在创建Game:GameData不断重复该语句。

Think about inheritance you want, as you are using it incorrectly with Mover too. 考虑一下您想要的继承,因为您在Mover上使用的继承也不正确。

Well you definitely have a circular reference which is a design smell. 好吧,您绝对有一个循环参考,这是设计的味道。 You could stop the infinite loop by checking for null : 可以通过检查null 停止无限循环:

public GameData()
{
    if(game == null)
        game = new Game();
}

Or pass the Game instance in to the constructor rather than creating a new one - hard to say what's correct without knowing more of your overall design. 还是将Game实例传递给构造函数,而不是创建一个新实例-很难说出正确的方法,而又不了解您的整体设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM