创建对象的引用 [英] Creating a reference to an object

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

问题描述

你好


我想创建一个对象的引用,以便对引用的对象的更改反映在另一个对象中。像这样:


对象o = 123;

对象p = o;


o = 456;


System.Console.WriteLine(" o = {0}",o);

System.Console.WriteLine(" p = {0} ,p);


456

123


我希望输出为


456

456

我该怎么做?


马克

Hello

I want to create a reference to an object, so that changes to the
referenced object are reflected in the other object. Like this:

object o = 123;
object p = o;

o = 456;

System.Console.WriteLine("o = {0}", o);
System.Console.WriteLine("p = {0}", p);

456
123

I would like the output to be

456
456

How do I do this?

Mark

推荐答案

ma **********@gmail.com 写道:

你好


我想要创建对象的引用,以便对引用的对象的更改反映在另一个对象中。像这样:


对象o = 123;

对象p = o;


o = 456;


System.Console.WriteLine(" o = {0}",o);

System.Console.WriteLine(" p = {0} ,p);


456

123


我希望输出为


456

456


我该怎么办?
Hello

I want to create a reference to an object, so that changes to the
referenced object are reflected in the other object. Like this:

object o = 123;
object p = o;

o = 456;

System.Console.WriteLine("o = {0}", o);
System.Console.WriteLine("p = {0}", p);

456
123

I would like the output to be

456
456

How do I do this?



您需要使用引用类型而不是值类型。


在您发布的代码中,您将从值类型(123,其是

type" int")。您可以将它分配给类型为object的变量,但只能分配给
,因为C#编译器会为您选择它,从值实例创建一个引用实例

。当您分配新值类型(456,再次

和int)时,该值实例再次被加框;创建的引用

替换变量o所持有的引用,而不是替换

盒装值o。引用。


换句话说,只有变量o。修改,而不是$ o $
指的是什么。


如果我记得,C#不允许你重载=运算符。所以

就像你在这里所做的那样,没有办法按字面意思编写代码。 =将

始终_replace_您分配给的变量的内容,

变量是否包含引用或值。


但是你当然可以实现你所说的更一般的目标:to

创建对象的引用,以便对引用的

对象进行更改反映在另一个对象中。


但是,要做到这一点,你需要有一个可变的引用类型,并且你需要修改现有的实例那种类型。任何引用

实例的地方都会看到这些修改。例如:


class MyMutable

{

int _num;


public MyMutable(int num)

{

_num = num;

}


public int Value

{

get {return _num; }

set {_num = value; }

}

}


无效主要()

{

MyMutable o = new MyMutable(123);

MyMutable p = o;


o.Value = 456;


Console.WriteLine(" o.Value = {0}",o.Value);

Console.WriteLine(" p.Value = {0}",p .Value);

}


这将输出:


o.Value = 456

p.Value = 456

Pete

You need to use a reference type rather than a value type.

In the code you posted, you start with a value type ("123", which is
type "int"). You can assign it to a variable of type "object", but only
because the C# compiler boxes it for you, creating a reference instance
from the value instance. When you assign a new value type ("456", again
an "int"), that value instance is again boxed; the reference created
replaces the reference held by variable "o", rather than replacing the
boxed value "o" referenced.

In other words, only the variable "o" is modified, rather than what "o"
refers to.

If I recall, C# doesn''t allow you to overload the = operator. So
there''s no way to write the code literally as you''ve done here. = will
always _replace_ the contents of the variable you''re assigning to,
whether the variable holds a reference or a value.

But you can certainly accomplish the more general goal you stated: "to
create a reference to an object, so that changes to the referenced
object are reflected in the other object".

To do that, however, you need to have a mutable reference type and you
need to modify an existing instance of that type. Any place where that
instance is referenced will then see those modifications. For example:

class MyMutable
{
int _num;

public MyMutable(int num)
{
_num = num;
}

public int Value
{
get { return _num; }
set { _num = value; }
}
}

void Main()
{
MyMutable o = new MyMutable(123);
MyMutable p = o;

o.Value = 456;

Console.WriteLine("o.Value = {0}", o.Value);
Console.WriteLine("p.Value = {0}", p.Value);
}

That will output:

o.Value = 456
p.Value = 456

Pete


感谢Pete。现在很清楚了。


Mark

Thanks for that Pete. It''s clear now.

Mark


9月27日上午11点16分,Peter Duniho < NpOeStPe ... @ NnOwSlPiAnMk.com>

写道:

[垃圾删除 - 开个玩笑]


这是但不是最后一个字......见下文。基本上Pete的

示例有点微不足道,因为p = o(相同)而不是作为循环中占位符的

,大多数程序需要另一个p的副本。下面

是一个使用浅层克隆的例子,其中p2表示p2。作为p $ / b $ b $的克隆(与o相同)。您可能还会考虑深度克隆如果您的类在其他类中存在并且很复杂,因为浅的

克隆引用与原始对象相同的对象,这将是

导致你的克隆被删除如果原始对象被删除了




RL


////////////////////////

使用System;

使用System.Collections。 Generic;

使用System.Text;


命名空间可变

{

class Program

{

static void Main(string [] args)

{

MyMutable o = new MyMutable(123);

MyMutable p = o;

// MyMutable p2 =(MyMutable)p.Clone(); //如果

放在这里给出123


o.Value = 456;

MyMutable p2 =(MyMutable)p.Clone (); //

放在这里时给出456


Console.WriteLine(" o.Value = {0}",o.Value);

Console.WriteLine(" o.hash is {0}",o.GetHashCode());

Console.WriteLine(" p.Value = {0} ",p.Value);

Console.WriteLine(" p.hash is {0}",p.GetHashCode());

// as当你运行这段代码时可以看到,p是与

o

Console.WriteLine(" p2.Value = {0}",p2}完全相同的对象。值);

Console.WriteLine(" p2.hash is {0}",p2.GetHashCode());

//可以看出p2是与p(和o)不同的对象

}

}

}


/ *

//以上输出

o.Value = 456

o.hash是58225482

p.Value = 456

p.hash是58225482

p2.Value = 456

p2.hash is 54267293

Press任何继续的关键。 。 。


* /

/////////////////////////// /////////////////////////////


使用系统;

使用System.Collections;

使用System.Collections.Generic;

使用System.Text;


namespace mutable

{

类MyMutable:ICloneable

{

int _num;


public MyMutable(int num)

{

_num = num;

}


public int Value

{

get {return _num; }

set {_num = value; } b / b
公共对象克隆()

{

return(this.MemberwiseClone()); //浅层克隆

}

}


}

/////// ///////

On Sep 27, 11:16 am, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.com>
wrote:
[Garbage deleted - just kidding]

This is not the last word however...see below. Essentially Pete''s
example is somewhat trivial, since p=o (identically) and other than as
a placeholder in a loop, most programs need another copy of p. Below
is an example using a shallow clone, with "p2" being a clone of p
(which is identical with o). You might also consider a deep clone if
your class inhereits other classes and is complex, since a shallow
clone references the same objects as the orginal object, which will
result in your clone being deleted if the original object gets
deleted.

RL

////////////////////////
using System;
using System.Collections.Generic;
using System.Text;

namespace mutable
{
class Program
{
static void Main(string[] args)
{
MyMutable o = new MyMutable(123);
MyMutable p = o;
//MyMutable p2 = (MyMutable)p.Clone(); //gives 123 if
placed here

o.Value = 456;
MyMutable p2 = (MyMutable)p.Clone(); //gives 456 when
placed here

Console.WriteLine("o.Value = {0}", o.Value);
Console.WriteLine("o.hash is {0}", o.GetHashCode());
Console.WriteLine("p.Value = {0}", p.Value);
Console.WriteLine("p.hash is {0}", p.GetHashCode());
//as can be seen when you run this code, p is the very same object as
o
Console.WriteLine("p2.Value = {0}", p2.Value);
Console.WriteLine("p2.hash is {0}", p2.GetHashCode());
// as can be seen p2 is a different object than p (and o)
}
}
}

/*
// output of above
o.Value = 456
o.hash is 58225482
p.Value = 456
p.hash is 58225482
p2.Value = 456
p2.hash is 54267293
Press any key to continue . . .

*/
////////////////////////////////////////////////////////

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace mutable
{
class MyMutable : ICloneable
{
int _num;

public MyMutable(int num)
{
_num = num;
}

public int Value
{
get { return _num; }
set { _num = value; }
}
public object Clone()
{
return (this.MemberwiseClone()); //shallow clone
}
}

}
//////////////


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

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