对象的引用 [英] References to objects

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

问题描述

我似乎还没有完全理解对象的引用。

比方说我这样做;


客户c =新客户();

客户c1 = c;


我明白如果我更改c1,我也会更改c,因为他们

都引用相同的数据。

但是看起来并非总是如此。

有时候我会把参考文献拿出去,改掉它然后去

回到原来仍然有它的旧价值。


什么时候这个引用复制适用于何时适用

not ?

我很困惑,因为我似乎得到了不同的结果。


我想知道的是什么时候它的规则/>
发生在什么条件下它不会发生。

例如,如果我有一个包含arraylist的对象

的客户,我抓住其中之一即:


foreach(客户c1 in theCase.Customers)

{

if(c1.Something == something)

myCustomer = c1;

}


myCustomer.Name ="不同的东西;


这现在是否也改变了theCase中的顾客?


关于这个主题,克隆方法是否给你一个干净的

版本,不会影响orignal?如果没有,我需要什么方法

复制?我将如何开始实施

这个?


非常感谢你们中的任何一位先生或女士们(有没有?b $ b女性编码员?)让我摆脱困境。


jax

解决方案

嗨Jax,


在线评论。


Joe
-
http://www.csharp-station.com


Jax <一个******* @ discussions.microsoft.com>在消息中写道

news:03 **************************** @ phx.gbl ... < blockquote class =post_quotes>我似乎还没有完全理解对象的引用。
让我们说例如我这样做;

客户c =新客户();
客户c1 = c;

我明白,如果我更改c1,我也会更改c,因为它们都引用同一条数据。


客户是一种参考类型(定义为一个类)。 c和c1都指向内存中相同对象的
。要使用精确的术语,您不会更改

c1,但是您正在更改c1引用的对象的状态。由于

c和c1都引用同一个对象,你可以通过两个引用查看对

对象的更改。

但这不是看起来总是如此。
有时候我会把参考文献拿出去,改掉它,然后再回到那个仍然具有旧价值的原版。


您可能正在谈论价值类型,它实际上将值从

复制到另一个变量。值类型可以通过关键字

" struct"来标识。在基类库(BCL)中的定义中使用。内置类型中的许多内容类型(如int和float)都是结构体,它们分别代替BCL类型

,如System.Int32和System.Single。字符串类型是

引用类型(类),并且是System.String BCL类型的别名。您定义为struct的任何自定义

类型也是值类型。另一方面,如果你将
定义为一个类,它是一个引用类型,你会看到上面描述的参考

行为。

什么时候引用复制适用,什么时候不适用?
我很困惑,因为我似乎得到了混合的结果。


使用引用类型,在赋值期间仅复制引用。使用

值类型,类型的值被复制。

我想知道的是规则何时发生以及在什么条件下发生它不会发生。
例如,如果我有一个对象包含一个arraylist
的客户,我抓住其中一个,即:

foreach(客户c1在案例中) 。客户)
{
if(c1.Something == something)
myCustomer = c1;
}

myCustomer.Name =" Something different" ;;

这现在是否也改变了theCase中的客户?


是的,假设客户是参考类型。

关于这个主题,克隆方法是否给你一个干净的
版本,不会影响原始?如果没有,我需要什么样的复制方法?我将如何开始实施这个?




克隆有两种方法。一种是使用Object.MemberwiseClone,

执行浅拷贝。浅拷贝意味着副本由调用MemberwiseClone的

立即对象组成。例如,如果你有
有一个对象A,它有一个对象B的引用,并且对C进行了一个MemberwiseClone

那么A和C都是具有复制状态的独立对象,但是他们

仍然会引用B,因为它是一个浅层副本。


要获得深层副本,你应该实现IClonable接口,

定义了一个克隆方法。一种方法是使用BinaryFormatter或

SoapFormatter来序列化类型。这样做的好处是你可以获得对象图中所有对象的深度副本,并且它避免了循环

,因为BCL格式化器足够智能,可以知道它们何时已经存在

序列化了一个对象。然后,您可以将流反序列化为新对象

并将其作为克隆对象返回。如果这样的方案对于你的

要求太多了,那么你可以根据需要提供克隆

方法的另一种实现方式。但是,您应该为使用您的类型的人记录

克隆的行为,以便他们知道会发生什么。这将是

帮助避免语义问题,当有人使用你的类型试图用深刻的副本来表示你的意思。


Joe


客户c;只是对Customer对象的引用,称为c。

new Customer();实际上是创建此对象的部分,一旦它创建了
,它就会返回地址。这个对象。


客户c =新客户();实际上是一行上的两个操作。

创建引用持有者c和一个Customer对象,它是无名且不可见的
只能通过引用访问

持有者,例如c。


客户c1 = c;也是一行上的两个操作。

首先它创建一个名为c1的新引用(holder),然后我们说c1'的

引用是相同的作为c'。

这样做的结果是c1和c都保持地址。对于相同的

对象(使用新的Customer();)创建,它们引用同一个客户。


ArrayList包含一个引用列表。 />

foreach(客户c1在theCase.Customers中)


//对于列表中的每个引用,将其视为客户参考并且

称之为c1


{

if(c1.Something == something)


// c1。访问它引用的对象的内容,在这种情况下

具有Something属性的Customer对象

//如果Something的值==某事,设置myCustomer参考

指向同一个对象c1指向


myCustomer = c1;

}


myCustomer.Name =" Something different";

// myCustomer。访问它引用的对象并将其名称

属性更改为Something different;


如果你重新使用foreach循环,你会发现对象c1 (当

c1.Something == something)引用时,其名称已更改。


这有一些例外。并非所有对象都是使用

''new''关键字创建的。

String line =" Hello World" ;;将创建一个新的String对象,并且

实际上与键入相同

String line = new String(" Hello World");

但''line''仍然只是对String对象的引用。对象

本身驻留在内存中的某个位置,一旦垃圾收集器发现

out没有引用指向此对象的它将收集它。

(line = null;将行的引用设置为除了创建的

String对象之外的其他内容,因此没有引用指向

String对象了。每个人都忘记了String对象和

垃圾收集器会很快发现并吃掉它,虽然没有人知道

的时候)


-

使用M2,Opera的革命性电子邮件客户端: http://www.opera.com/m2/





简单的概念是如果你引用了一个参考

类型,那么改变一个值会影响

其他。另一方面,如果您引用了一个

值类型,那么它将始终复制

对象,从而对一个对象进行更改不要改变

另一个。


要了解更多关于参考类型和价值类型的信息,请检查

以下链接。

http://msdn.microsoft.com/library/default.asp?

url = / library / en-us / vbcn7 / html / vbconvaluereftypes.asp


希望这有帮助...


问候,

Madhu

-----原始消息-----
我似乎还没有完全理解对象
的引用。让我们说比如我这样做;

客户c =新客户();
客户c1 = c;

我明白如果我更改c1,我也将c更改为
他们都引用同一条数据。
但是看起来并不总是如此。
有时候我会把参考文献拿出去,改掉它然后再回到原来的那个仍然有它的旧版本。价值。

什么时候引用复制适用,什么时候不适用?
我很困惑,因为我似乎得到了混合的结果。

我想知道的是规则说明
是什么时候以及它在什么条件下它不会发生。
例如,如果我有一个包含
arraylistof客户的对象,我抓住其中一个,即:

foreach(客户c1 in theCase.Customers) {
if(c1.Something == something)
myCustomer = c1;
}

myCustomer.Name =" Something different" ;;

这现在是否也改变了theCase中的客户?

关于这个主题,克隆方法是否给你一个不会影响orignal的
cleanversion?如果没有,我需要什么
的复制方法,我将如何开始实现
呢?

非常感谢你们任何一位先生或女士们(这些都是
therefemale编码员?)让我摆脱困境。

jax



I dont seem to fully comprehend references to objects yet.
Lets say for example I do this;

Customer c = new Customer();
Customer c1 = c;

I understand that if I change c1, I also change c as they
are both referencing the same piece of data.
But this isn''t always the case it would seem.
Sometimes I take a reference out, change it and then go
back to the original which still has it''s old value.

When does this reference copying apply and when does it
not?
I''m confused as I seem to be getting mixed results.

What I would like to know are the rules that state when it
happens and under what conditions it doesn''t happen.
For example if I have an object that contains an arraylist
of customers and I grab one of those i.e:

foreach(Customer c1 in theCase.Customers)
{
if(c1.Something == something)
myCustomer = c1;
}

myCustomer.Name = "Something different";

Does this now change the customer in theCase as well?

Also on this topic, does the clone method give you a clean
version that wont effect the orignal? If not, what method
of copying would I need and how would I begin to implement
this?

Many thanks for any of you kind sirs or madames (are there
female coders?) who get me out of my muddle.

jax

解决方案

Hi Jax,

Comments in-line.

Joe
--
http://www.csharp-station.com

"Jax" <an*******@discussions.microsoft.com> wrote in message
news:03****************************@phx.gbl...

I dont seem to fully comprehend references to objects yet.
Lets say for example I do this;

Customer c = new Customer();
Customer c1 = c;

I understand that if I change c1, I also change c as they
are both referencing the same piece of data.
Customer is a reference type (defined as a class). Both c and c1 refer to
the same object in memory. To use precise terminology, you are not changing
c1, but you are changing the state of the object that c1 refers to. Since
both c and c1 refer to the same object, you can view the changes to that
object through both references.
But this isn''t always the case it would seem.
Sometimes I take a reference out, change it and then go
back to the original which still has it''s old value.
You could be talking about value types, which actually copy the values from
one variable to another. Value types can be identified by the keyword
"struct" used in their definition in the Base Class Library (BCL). Many of
the built-in types, such as int and float are structs, which alias BCL types
such as System.Int32 and System.Single, respectively. The string type is a
reference type (class) and aliases the System.String BCL type. Any custom
type you define as struct is also a value type. On the other hand, if you
define a type as a class, it is a reference type and you will see reference
behavior described above.
When does this reference copying apply and when does it
not?
I''m confused as I seem to be getting mixed results.
With reference types, only the reference is copied during assignment. With
value types, the value of the type is copied.
What I would like to know are the rules that state when it
happens and under what conditions it doesn''t happen.
For example if I have an object that contains an arraylist
of customers and I grab one of those i.e:

foreach(Customer c1 in theCase.Customers)
{
if(c1.Something == something)
myCustomer = c1;
}

myCustomer.Name = "Something different";

Does this now change the customer in theCase as well?
Yes, assuming that Customer is a reference type.
Also on this topic, does the clone method give you a clean
version that wont effect the orignal? If not, what method
of copying would I need and how would I begin to implement
this?



There are two ways to clone. One is to use the Object.MemberwiseClone,
which does a shallow copy. A shallow copy means that a copy is made of the
immediate object that MemberwiseClone is called on. For instance if you
have an object A that has a reference to object B and do a MemberwiseClone
to C then both A and C will be separate objects with copied state, but they
will still both refer to B because it was a shallow copy.

To get a deep copy, you should implement the IClonable interface, which
defines a Clone method. One way to do this is to use a BinaryFormatter or
SoapFormatter to serialize the type. The benefits of this are that you get
a deep copy of all objects in the object graph and it avoids circularities
because the BCL formatters are smart enough to know when they have already
serialized an object. You can then deserialize the stream into a new object
and return that as the cloned object. If such a scheme is too much for your
requirements, then you can provide another implementation of the Clone
method as you see fit. However, you should document the behavior of the
Clone for people using your type so they know what to expect. This will
help avoid semantic issues when someone using your type is trying to figure
out what you mean by a deep copy.

Joe


Customer c; is just a reference to a Customer object which is called c.
new Customer(); is actually the part that creates this object and once it
is created it returns the "address" to this object.

Customer c = new Customer(); is actually two operations on a single line.
The creation of the reference holder c and the a Customer object which is
nameless and invisible and can only be accessed through a reference
holder, such as c.

Customer c1 = c; is also two operations on a single line.
First it creates a new reference (holder) called c1, then we say that c1''s
reference is the same one as c''s.
The effect of this is that both c1 and c holds the "address" to the same
object (created with new Customer();), they reference the same Customer.

An ArrayList holds a list of references.

foreach(Customer c1 in theCase.Customers)

// for each reference in the list, treat it as a Customer reference and
call it c1

{
if(c1.Something == something)

// c1. accesses the content of the object it references to, in this casea
Customer object which has a Something property
// if the value of Something == something, set the myCustomer reference to
point the same object c1 points to

myCustomer = c1;
}

myCustomer.Name = "Something different";
// myCustomer. accesses the object it references and changes its Name
property to "Something different";

If you reran the foreach loop you would find that the object c1 (when
c1.Something == something) references to has its Name changed.

There are a few exceptions to this. Not all objects are created with the
''new'' keyword.
String line = "Hello World"; will create a new String object and is
actually the same as typing
String line = new String("Hello World");
but ''line'' is still just a reference to a String object. The object
itself resides somewhere in memory and once the garbage collector finds
out there are no references pointing to this object it will collect it.

(line = null; will set the reference of line to something other than the
String object that was created, so there is no reference pointing to the
String object anymore. Everyone has forgotten about the String object and
the garbage collector will spot it soon and eat it, though nobody knows
when)

--
Using M2, Opera''s revolutionary e-mail client: http://www.opera.com/m2/


Hi,

Simple concept is if you have a reference to a reference
type then changing the value in one will affect the
other. On the other hand, if you have reference to a
value type, then it will always make a copy of the
object, thus making a change in one object doesn''t change
the other.

To know more about Reference type and value type check
out the following link.

http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/vbcn7/html/vbconvaluereftypes.asp

Hope this helps...

Regards,
Madhu

-----Original Message-----
I dont seem to fully comprehend references to objects yet.Lets say for example I do this;

Customer c = new Customer();
Customer c1 = c;

I understand that if I change c1, I also change c as theyare both referencing the same piece of data.
But this isn''t always the case it would seem.
Sometimes I take a reference out, change it and then go
back to the original which still has it''s old value.

When does this reference copying apply and when does it
not?
I''m confused as I seem to be getting mixed results.

What I would like to know are the rules that state when ithappens and under what conditions it doesn''t happen.
For example if I have an object that contains an arraylistof customers and I grab one of those i.e:

foreach(Customer c1 in theCase.Customers)
{
if(c1.Something == something)
myCustomer = c1;
}

myCustomer.Name = "Something different";

Does this now change the customer in theCase as well?

Also on this topic, does the clone method give you a cleanversion that wont effect the orignal? If not, what methodof copying would I need and how would I begin to implementthis?

Many thanks for any of you kind sirs or madames (are therefemale coders?) who get me out of my muddle.

jax
.



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

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