如何引用类中的对象? [英] How to have a reference to an object in a class?

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

问题描述

是否可以在类中引用对象?如果是这样,

语法是什么?我想这样做的原因是我可以制作原始对象的副本,修改副本然后,如果

修改是可以接受的,使用修改后的副本对象的内容重新加载原始对象。


如果我在C ++中这样做,我只会传入一个指针到原来的

对象,将其构造函数中的内容复制到临时对象,对临时对象进行操作,并在最终确认后复制
temp对象通过传入的指针指向原始对象。我确定

这样的事情在C#中是可能的,但我不能得到语法

吧。如何挂起原始参考?我不知道如何将
声明为数据成员作为参考。当然,我可以通过

来解决这个问题,从临时对象复制到

调用者代码中的原始对象,但是我希望将所有内容封装在这个类而不是
强制调用者做任何超出必要的事情。


示例:

假设O1是对象这是要修改的。

假设C1是负责处理

修改的类。

当创建C1时,它获取对原始O1的引用。

将原始O1复制到C1内的临时O1。

C1有两种可能的退出条件。

退出成功 - 修改后的O1的内容被复制到原来的

O1。

退出失败 - 原来的O1未经修改。

-

Richard Lewis Haggard
www .Haggard-And-Associates.com

解决方案

Richard Lewis Haggard写道:

是否可以在类中引用对象?如果是这样,
是什么语法?我想这样做的原因是我可以制作原始对象的副本,对副本进行修改然后,如果
修改是可接受的,则用内容<重新加载原始对象<修改后的副本对象。

如果我在C ++中这样做,我只需要传入指向原始对象的指针,将其内容复制到构造函数中临时对象,对临时对象进行操作,并在最终确认后,通过传入的指针将
临时对象的内容复制到原始对象。我确定在C#中这样的东西是可能的,但是我不能正确地获得语法
。如何挂起原始参考?我没有看到如何声明数据成员作为参考。当然,我可以通过
从临时对象复制到
调用者代码中的原始对象来解决这个问题,但是我希望将所有内容封装在类中而不是
强制调用者做任何超出必要的操作。

示例:
假设O1是要修改的对象。
假设C1是负责的类处理
修改。
当创建C1时,它会获得对原始O1的引用。
原始O1被复制到C1内部的临时O1中。
C1有两个可能的退出条件。
退出成功 - 修改后的O1的内容被复制到原来的
O1。
退出失败 - 原来的O1未经修改。




听起来你想要在

IClone中实现克隆方法。


John


使用ref应该这样做,如下例所示。


class Exec

{

static void Main()

{

MyClass m = new MyClass();

MyObject ob = new MyObject();

ob.gem = 1 ;

bool changed = m.ChangeObj(ref ob);

Console.WriteLine(" ob.gem = {0}",ob.gem); < br $>
}

}

class MyClass

{

//如果是原始对象被更改

public bool ChangeObj(ref MyObject o)

{

o.gem ++; //更改对象

if(o.gem> 1)//制作标准

{

o.gem-- ; //恢复对象

返回false;

}

else

返回true;

}

}

class MyObject

{

public int gem;

private int sowsEar;

}


干杯,

技术博士


Tech Dr.< te ***** @ earthlink.net>写道:

使用" ref"应该这样做,如下例所示。




我认为你误解了ref的目的。您的

示例中没有任何内容需要使用ref。


请参阅 http://www.pobox.com/~skeet/csharp/parameters.html


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复该组,请不要给我发邮件


Is it possible to put a reference to an object inside of a class? If so,
what is the syntax? The reason I want to do this is so that I can make a
copy of the original object, make modifications to the copy and then, if the
modifications are acceptable, reload the original object with the contents
of the modified copy object.

If I were doing this in C++, I''d just pass in a pointer to the original
object, copy its contents in the constructor to a temporary object, operate
on the temp object and, upon final confirmation, copy the contents of the
temp object to the original object through the passed in pointer. I''m sure
that something like this is possible in C# but I just can''t get the syntax
right. How does one hang on to the original reference? I don''t see how to
declare a data member as a reference. Granted, I can get around that by
doing a copy from the temp object back to the original object in the
caller''s code but I want to encapsulate everything in the class and not
force the caller to do anything more than necessary.

Example:
Assume that O1 is the object that is to be modified.
Assume that C1 is the class that is responsible for handling the
modification.
When C1 is created, it gets a reference to the original O1.
The original O1 is copied into a temporary O1 inside of C1.
C1 has two possible exit conditions.
Exit successful - the contents of the modified O1 are copied to the original
O1.
Exit failure - the original O1 is left unmodified.
--
Richard Lewis Haggard
www.Haggard-And-Associates.com

解决方案

Richard Lewis Haggard wrote:

Is it possible to put a reference to an object inside of a class? If so,
what is the syntax? The reason I want to do this is so that I can make a
copy of the original object, make modifications to the copy and then, if the
modifications are acceptable, reload the original object with the contents
of the modified copy object.

If I were doing this in C++, I''d just pass in a pointer to the original
object, copy its contents in the constructor to a temporary object, operate
on the temp object and, upon final confirmation, copy the contents of the
temp object to the original object through the passed in pointer. I''m sure
that something like this is possible in C# but I just can''t get the syntax
right. How does one hang on to the original reference? I don''t see how to
declare a data member as a reference. Granted, I can get around that by
doing a copy from the temp object back to the original object in the
caller''s code but I want to encapsulate everything in the class and not
force the caller to do anything more than necessary.

Example:
Assume that O1 is the object that is to be modified.
Assume that C1 is the class that is responsible for handling the
modification.
When C1 is created, it gets a reference to the original O1.
The original O1 is copied into a temporary O1 inside of C1.
C1 has two possible exit conditions.
Exit successful - the contents of the modified O1 are copied to the original
O1.
Exit failure - the original O1 is left unmodified.



It sounds me that you want to look into implementing the clone method in
IClone.

John


Using "ref" should do it, as in the following example.

class Exec
{
static void Main()
{
MyClass m = new MyClass();
MyObject ob = new MyObject();
ob.gem = 1;
bool changed = m.ChangeObj(ref ob);
Console.WriteLine("ob.gem = {0}", ob.gem);
}
}
class MyClass
{
// returns true if the original object is changed
public bool ChangeObj( ref MyObject o)
{
o.gem++; // change the object
if (o.gem > 1) // made-up criterion
{
o.gem--; // restore object
return false;
}
else
return true;
}
}
class MyObject
{
public int gem;
private int sowsEar;
}

Cheers,
Tech Dr.


Tech Dr. <te*****@earthlink.net> wrote:

Using "ref" should do it, as in the following example.



I think you''ve misunderstood the purpose of "ref". Nothing in your
example requires the use of ref.

See http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


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

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