实例访问其他实例的 属性 个值
instance accesses property value of other instances
我的目的是从用户界面获取数据并将其保存为配置。该程序允许配置多人,所以我试图保存每个人的设置。我意识到了一些事情。每当我更改一个实例的数据时,其他实例的值都会发生变化。
public class Person()
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public void Change()
{
Name = "F";
}
}
这是我的人class
这是我的主
static void Main(string[] args)
{
Person person = new Person();
Person person2 = new Person();
Person current = new Person();
person.Name = "John";
person2.Name = "Doe";
current.Name = "Robert";
person = current;
person2 = current;
current.Change()
}
在 current.Change() 之后,人 1 和人 2 的名字更改为 "F"。这种行为让我感到困惑,因为我希望在
上得到这个结果
person = current;
person2 = current;
current.Change()
person = current;
person2 = current;
我想要的是在界面上使用电流。获取数据并将它们保存到人员实例中。问题是,当我更改当前属性时,所有其他实例属性都更改为相同,并且我丢失了我的配置设置。
我怎样才能达到我的目的?有什么想法吗?
非常感谢!抱歉英语不好。
因为 Person
是一个引用类型,当你做 person2 = current;
你实际上只是将 person2
引用指向内存中的 current
对象,这是person
.
也指出
因此,通过一个引用所做的任何更改都将更改内存中的同一物理对象,就像通过另一个引用所做的任何更改。
您需要做的是制作对象的实际副本。
一种常见的方法是写一个"Copy constructor" 来制作一个副本。您还可以编写一个 Copy()
方法,该方法使用复制构造函数来实现,但这并不是真正必要的,除非您具有继承层次结构。
这是你的 Person
class 使用复制构造函数和(为了完整性) Copy()
方法的样子:
public class Person
{
private string _name;
public Person() // Default constructor is required if there are any other constructors!
{
Name = "";
}
public Person(Person other) // Copy constructor.
{
this.Name = other.Name;
}
public Person Copy()
{
return new Person(this); // Use copy constructor to return a copy of this.
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public void Change()
{
Name = "F";
}
}
然后您将按如下方式使用它作为您的示例:
static void Main()
{
Person person = new Person();
Person person2 = new Person();
Person current = new Person();
person.Name = "John";
person2.Name = "Doe";
current.Name = "Robert";
person = new Person(current); // Using Copy Constructor.
person2 = current.Copy(); // Using Copy() method.
current.Change(); // Only changes current, not person or person2.
}
注意:我建议您不要实现 Copy()
方法,而是坚持使用复制构造函数,除非(如前所述)您的 class 有一个继承层次结构。那将是 advanced usage beyond the scope of this answer!
我的目的是从用户界面获取数据并将其保存为配置。该程序允许配置多人,所以我试图保存每个人的设置。我意识到了一些事情。每当我更改一个实例的数据时,其他实例的值都会发生变化。
public class Person()
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public void Change()
{
Name = "F";
}
}
这是我的人class
这是我的主
static void Main(string[] args)
{
Person person = new Person();
Person person2 = new Person();
Person current = new Person();
person.Name = "John";
person2.Name = "Doe";
current.Name = "Robert";
person = current;
person2 = current;
current.Change()
}
在 current.Change() 之后,人 1 和人 2 的名字更改为 "F"。这种行为让我感到困惑,因为我希望在
上得到这个结果 person = current;
person2 = current;
current.Change()
person = current;
person2 = current;
我想要的是在界面上使用电流。获取数据并将它们保存到人员实例中。问题是,当我更改当前属性时,所有其他实例属性都更改为相同,并且我丢失了我的配置设置。
我怎样才能达到我的目的?有什么想法吗?
非常感谢!抱歉英语不好。
因为 Person
是一个引用类型,当你做 person2 = current;
你实际上只是将 person2
引用指向内存中的 current
对象,这是person
.
因此,通过一个引用所做的任何更改都将更改内存中的同一物理对象,就像通过另一个引用所做的任何更改。
您需要做的是制作对象的实际副本。
一种常见的方法是写一个"Copy constructor" 来制作一个副本。您还可以编写一个 Copy()
方法,该方法使用复制构造函数来实现,但这并不是真正必要的,除非您具有继承层次结构。
这是你的 Person
class 使用复制构造函数和(为了完整性) Copy()
方法的样子:
public class Person
{
private string _name;
public Person() // Default constructor is required if there are any other constructors!
{
Name = "";
}
public Person(Person other) // Copy constructor.
{
this.Name = other.Name;
}
public Person Copy()
{
return new Person(this); // Use copy constructor to return a copy of this.
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public void Change()
{
Name = "F";
}
}
然后您将按如下方式使用它作为您的示例:
static void Main()
{
Person person = new Person();
Person person2 = new Person();
Person current = new Person();
person.Name = "John";
person2.Name = "Doe";
current.Name = "Robert";
person = new Person(current); // Using Copy Constructor.
person2 = current.Copy(); // Using Copy() method.
current.Change(); // Only changes current, not person or person2.
}
注意:我建议您不要实现 Copy()
方法,而是坚持使用复制构造函数,除非(如前所述)您的 class 有一个继承层次结构。那将是 advanced usage beyond the scope of this answer!