简体   繁体   English

C#创建一个Class,将对象作为成员变量? 我认为对象是垃圾收集?

[英]C# creating a Class, having objects as member variables? I think the objects are garbage collected?

So I have a class that has the following member variables. 因此,我有一个具有以下成员变量的类。 I have get and set functions for every piece of data in this class. 我为此类中的每个数据获取并设置了函数。

public class NavigationMesh
{
    public Vector3 node;
    int weight;
    bool isWall;
    bool hasTreasure;

    public NavigationMesh(int x, int y, int z, bool setWall, bool setTreasure)
    {
        //default constructor
        //Console.WriteLine(x + " " + y + " " + z);
        node = new Vector3(x, y, z);

        //Console.WriteLine(node.X + " " + node.Y + " " + node.Z);

        isWall = setWall;
        hasTreasure = setTreasure;

        weight = 1;

    }// end constructor

    public float getX()
    {
        Console.WriteLine(node.X);
        return node.X;
    }
    public float getY()
    {

        Console.WriteLine(node.Y);
        return node.Y;
    }
    public float getZ()
    {
        Console.WriteLine(node.Z);
        return node.Z;
    }

    public bool getWall()
    {
        return isWall;
    }

    public void setWall(bool item)
    {
        isWall = item;
    }

    public bool getTreasure()
    {
        return hasTreasure;
    }


    public void setTreasure(bool item)
    {
        hasTreasure = item;
    }

    public int getWeight()
    {
        return weight;
    }

}// end class

In another class, I have a 2-Dim array that looks like this 在另一堂课中,我有一个二维数组,看起来像这样

NavigationMesh[,] mesh; NavigationMesh [,]网格;

mesh = new NavigationMesh[502,502]; mesh =新的NavigationMesh [502,502];

I use a double for loop to assign this, my problem is I cannot get the data I need out of the Vector3 node object after I create this object in my array with my "getters". 我使用double for循环来分配它,我的问题是在用“ getters”在数组中创建此对象后,无法从Vector3节点对象中获取所需的数据。

I've tried making the Vector3 a static variable, however I think it refers to the last instance of the object. 我曾尝试使Vector3成为静态变量,但是我认为它指向对象的最后一个实例。 How do I keep all of these object in memory? 如何将所有这些对象保留在内存中? I think there being garbage collected. 我认为那里有垃圾收集。 Any thoughts? 有什么想法吗?

In C#, don't build get and set functions like that. 在C#中,不要像这样构建get和set函数。 Use a property: 使用属性:

public float X
{
   get {Console.WriteLine(node.X); return X;}
}

public bool IsWall {get;set;}

If you have a reference to these items, they are not garbage collected. 如果您引用这些项目,则不会将它们收集为垃圾。

If you are maintaining a reference to them, they are guaranteed to NOT get garbage collected . 如果您要保留对它们的引用,则可以保证它们不会被垃圾收集

I don't see where you are defining node . 我看不到您在哪里定义node Is it possible that another piece of code is interfering with it? 另一段代码是否有可能干扰它? Is it declared static? 是否声明为静态? If it is declared static, there is only one instance of it (instead of one per class), and this could be the source of some confusion. 如果将其声明为静态,则只有一个实例(而不是每个类一个),这可能会引起一些混乱。

Why is your node field declared public? 为什么将您的node字段声明为公共?

I would put that in a property, and set a breakpoint on the set accessor, so that you can see where it is getting modified. 我将其放在一个属性中,并在设置的访问器上设置一个断点,以便您可以看到它在哪里被修改。

Vector3 is a struct, so it is not an object. Vector3是结构,因此它不是对象。 It is a value. 这是一个价值。 This means that it is definitely not being garbage collected. 这意味着它绝对不会被垃圾回收。

It is a value type, so every time you pass it along a copy is made. 这是一个值类型,因此每次传递副本时都会进行一次。 Could it be that you get a copy and modify that? 能否获得副本并进行修改? It is not clear to me what you mean, when you say you can't get data out of node. 我不清楚您的意思是什么,当您说无法从节点中获取数据时。

In the class definition everything seems kosher to me except the fact the the node field is declared public - you should avoid this, but I hope you do not assign null to it. 在类定义中,除了node字段被声明为public以外,其他一切对我来说似乎都是洁白的-您应该避免这种情况,但是我希望您不要为其分配null。
If you do not your Vector object will not be garbage collected. 如果不这样做,将不会对Vector对象进行垃圾收集。

So what exactly is happening when you try to access the getters? 那么当您尝试访问吸气剂时到底发生了什么? Does GetX throw a null pointer exception? GetX会抛出空指针异常吗?

We cannot see what code you are using to instantiate the 502 by 502 array: Are you sure you are instantiating a new node for every one? 我们看不到您正在使用什么代码实例化502 by 502数组:您确定要为每个实例实例化一个新节点吗? If yes, than you should easily be able to retrieve the properties. 如果是,那么您应该可以轻松地检索属性。 mesh[x,y].getWall() would give you the correct value for given x's and y's. mesh [x,y] .getWall()将为给定的x和y提供正确的值。

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

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