pen*_*ake 4 .net c# java design-patterns
当我使用引用类型时,你能建议我一种方法可以防止我出现这种情况吗?
var someCost = new Cost( Price: new Price(1000, "USD")
, CostType: "Type-A") ;
var candyCost = new Cost();
// Initialize candyCost.Price
candyCost.Price = someCost.Price; //Now candyCost Price is referencing
// price of someCost;
// (.......)
// Some logic and code here
//and I forgot that I was referencing to someCost object's price object
//and I do stupid mistake:
candyCost.Price.Value = 5000; //Now I believe I have updated candyCost price
//but I have also updated someCost!!
Run Code Online (Sandbox Code Playgroud)
其余的故事是关于调试以找出为什么someCost的价格更新.
我想通过这个例子简化问题.我希望你明白我的意思.
问题:你能否建议我一种方法来避免重复这样的错误?更新引用类型的值时的任何设计模式.
| 归档时间: |
|
| 查看次数: |
111 次 |
| 最近记录: |