类实例将覆盖另一个实例 [英] Class instances overwrites another instance

查看:80
本文介绍了类实例将覆盖另一个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我有个问题要问.

我有这个班.

Hey everyone.

I''ve question to ask.

I have this class.

public class Node
{
    public int Kova1;               // Kova 1
    public int Kova2;               // Kova 2
    public int Kova3;               // Kova 3

    public int ActionNo;            // Yapılan İşlem

    public Node(int kova1, int kova2, int kova3, int actionNumber)
    {
        Kova1 = kova1;
        Kova2 = kova2;
        Kova3 = kova3;
        ActionNo = actionNumber;
    }

    public Node(int kova1, int kova2, int kova3)
    {
        Kova1 = kova1;
        Kova2 = kova2;
        Kova3 = kova3;
    }

    public Node()
    {
    }

    public Node AnneNode;
}



每当我创建此类的实例并对该实例执行一些操作时,它都会影响所有其他实例.

我该如何克服?

最好的问候..



Whenever I create an instance of this class and do some operation on that instance, it affects all the other instances.

How can I overcome this?

My best regards..

推荐答案

此陈述不正确.(尽管这是政治上正确的"表达.:-))

实际上,没有实例覆盖实例"之类的东西.可能是同一实例,也可能不是.您的代码中没有任何东西会引起交叉修改,也没有静态代码.因此,我必须得出结论,不会发生覆盖".只是您的实验"无效或您的观察而已,但您未提供任何信息.

我严重怀疑您不了解非常非常非常基本的内容.您是否真的了解该类是引用类型而不是值类型?变量不是通过赋值复制"的,而是引用相同的对象.考虑:
This statement is not true. (This a "politically correct" expression though. :-))

There is no such thing as "instance overwriting the instance", actually. It''s either the same instance or not. There is nothing in your code which would cause any cross-modification, and there is no static code. So, I must conclude, no "overwriting" takes place. It''s just your "experiment" is invalid, or your observations, but you did not provide any information.

I seriously suspect you don''t understand something very, very, very basic. Do you really understand that the class is the reference type and not a value type? The variable are not "copied" by assignment but reference the same object. Consider:
Node first = new Node();
Node second = new Node();
second.Kova1 = 1; //will not effect first

//but
second = first; // no more old second object anymore; first and second simply reference the same object, so:
second.Kova2 = 2; // first.Kova2 becomes 2, but not because the object "overwrites" anything, but just because there are two references to the SAME object



请回到最基础的地方.

另外,代码存在一些问题:

根本不需要构造函数public Node() {}.

可能不需要public声明.如果要在同一程序集中使用某些类型的成员,则最好将其设为internal.这与public相同,仅适用于同一程序集.您不应允许超出实际需要的访问权限.

在大多数情况下,publicinternal类型字段表示样式错误.为了获得更好的代码可维护性,请将所有字段都设为private,并仅将它们抛出属性.这些属性可以具有任何访问修饰符.首先,将所有字段转换为自动支持的属性,这样以后您将有机会为其编写一些特定的getter或setter.

祝你好运,

—SA



Please, get back to the very basics.

Also, some problems with the code:

The constructor public Node() {} is not needed at all.

The public declarations may not be needed. If you are going to use some types of members in the same assembly, they should better be internal. This is the same as public, only for the same assembly. You should not allow more access than this is really needed.

In most cases, the public or internal type fields means bad style. For better code maintainability, make all fields private and expose them only throw properties. The properties can have any access modifiers. At first, turn all fields into auto-backed properties, so later you will get an opportunity to write some specific getters or setters for them.

Good luck,

—SA


这篇关于类实例将覆盖另一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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