将对象作为副本传递 [英] pass an object as a copy

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

问题描述

我有如下情况

I have a case as follows

class nurses
{
...
...
}
class shifts
{
nurses _nurses;
shifts(nurses _nurses)
{
this._nurses=_nurses;// here the reference of nurses is assigned not the value.
}
...
...
}
main()
{
var ns=new nurses();
var shfts= new shifts(ns);//I want to pass the ns as copy so the value of ns is not changed after shifts make changes on its copy.


}



请提出一些快速解决方案.这让我感到困惑.
谢谢



Please suggest some quick solution. It is confusing to me.
Thank you

推荐答案

您可以将nurses定义为struct,使其成为值类型:

http://msdn.microsoft.com/en-us/library/ah19swz4%28v = vs.71%29.aspx [ ^ ]

或者您可以在nurses上实现IClonable来创建副本:

http://msdn.microsoft.com/en-us/library/system.icloneable.aspx [^ ]
You can either define nurses as a struct, making it a value type:

http://msdn.microsoft.com/en-us/library/ah19swz4%28v=vs.71%29.aspx[^]

or you can implement IClonable on nurses to create a copy:

http://msdn.microsoft.com/en-us/library/system.icloneable.aspx[^]


使用原型设计模式 http://dofactory.com/Patterns/PatternPrototype.aspx [ ^ ]
Use Prototype design pattern http://dofactory.com/Patterns/PatternPrototype.aspx[^]


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

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