Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 方法对类的所有实例运行_Java - Fatal编程技术网

Java 方法对类的所有实例运行

Java 方法对类的所有实例运行,java,Java,我遇到了这个问题,这让我发疯。简而言之,我实例化了同一类的两个对象。当我在一个对象中运行一个方法时,另一个对象也会受到影响,就像我在第二个对象上显式调用了一个方法一样。我想知道是否有人能帮我一下 假设,我有班级档案 然后我会分配到一个投资组合领域,一些工具 p.portfolio = someInstrumentList; 然后,我将按价值将投资组合p复制到pCopy中: 所以现在我有两个相同的物体。另外一个是“按值复制”对象。更改pCopy中字段的值不会影响p中的相同字段 现在,当我在p上运

我遇到了这个问题,这让我发疯。简而言之,我实例化了同一类的两个对象。当我在一个对象中运行一个方法时,另一个对象也会受到影响,就像我在第二个对象上显式调用了一个方法一样。我想知道是否有人能帮我一下

假设,我有班级档案

然后我会分配到一个投资组合领域,一些工具

p.portfolio = someInstrumentList;
然后,我将按价值将投资组合p复制到pCopy中:

所以现在我有两个相同的物体。另外一个是“按值复制”对象。更改pCopy中字段的值不会影响p中的相同字段

现在,当我在p上运行applyStress方法时,pCopy中的仪器列表的值也会改变

换句话说,如果p.portfolio.get0==1,那么在p.applyStress之后,我希望看到p.portfolio.get0是2,pCopy.portfolio.get0是1

但是我看到的是p.portfolio.get0是2,pCopy.portfolio.get0也是2


我不明白为什么会这样。这不是静态修饰符的问题,因为没有静态修饰符。有人有什么想法吗?

应用于您的ArrayList引用的克隆方法是a,而不是a。这意味着您在原始集合中拥有的任何内容都将被克隆的集合共享

这意味着您还需要克隆每个工具,或者为每个工具提供一个副本构造函数

this.portfolio = new ArrayList<Instrument>();
for(Instrument toBeCopiedInstrument : copyFrom.portfolio){
   this.portfolio.add(new Instrument(toBeCopiedInstrument ));
}

应用于您的ArrayList引用的clone方法不包含,而不是。这意味着您在原始集合中拥有的任何内容都将被克隆的集合共享

这意味着您还需要克隆每个工具,或者为每个工具提供一个副本构造函数

this.portfolio = new ArrayList<Instrument>();
for(Instrument toBeCopiedInstrument : copyFrom.portfolio){
   this.portfolio.add(new Instrument(toBeCopiedInstrument ));
}
默认情况下,clone执行所谓的浅层复制,这意味着它只复制对要克隆的列表中所包含对象的引用,而实际上不会将对象本身复制到新实例

您需要做的是为列表和列表中的每个项目实现一个自定义深度副本。但是深度克隆在Java中是一个不完整的概念和实现

复制构造函数在Java中也不是一个很好的模式,因为在大多数情况下,您最终也会复制引用,并且您注入到构造函数中的每个对象都必须遵循相同的复制构造函数语义。与C++不同,这是手动、繁琐、不可维护和容易出错的过程!p> .clone和implements Cloneable是在Java中获取正确概念最复杂的工具之一。在设计良好的应用程序中很少需要它们。如果对对象进行逐位复制是除存储之外的其他设计的一部分,则可能需要重新审视您的设计

对象的克隆方法非常棘手。它基于现场副本,并且 这是语言外的。它创建一个对象而不调用 构造器。不能保证它保留不变量 由施工人员建立。在过去的一段时间里有很多错误 年,无论是在阳光下还是在阳光下 只需重复调用super.clone,直到您克隆了一个 对象,则有对象的浅副本。克隆人 与正在克隆的对象共享状态。如果这个状态是可变的, 没有两个独立的对象。如果修改其中一个,则另一个 变化也是如此。突然间,你会有随机行为

不变的

一个更好的模式是使一切都不可变。这样,您就不需要单独的实例,您可以共享实例,直到它们需要更改,然后它们更改,您就有了一个包含新数据的新实例,该实例可以在没有任何副作用的情况下共享。

默认情况下。克隆执行所谓的浅层复制,这意味着它只复制对要克隆的列表中所包含对象的引用,而不会将对象本身复制到新实例中

您需要做的是为列表和列表中的每个项目实现一个自定义深度副本。但是深度克隆在Java中是一个不完整的概念和实现

复制构造函数在Java中也不是一个很好的模式,因为在大多数情况下,您最终也会复制引用,并且您注入到构造函数中的每个对象都必须遵循相同的复制构造函数语义。与C++不同,这是手动、繁琐、不可维护和容易出错的过程!p> .clone和implements Cloneable是在Java中获取正确概念最复杂的工具之一。在设计良好的应用程序中很少需要它们。如果对对象进行逐位复制是除存储之外的其他设计的一部分,则可能需要重新审视您的设计

对象的克隆方法非常棘手。它基于现场副本,并且 这是语言外的。它创建一个对象而不调用 构造器。有 不能保证它保留不变量 由施工人员建立。在过去的一段时间里有很多错误 年,无论是在阳光下还是在阳光下 只需重复调用super.clone,直到您克隆了一个 对象,则有对象的浅副本。克隆人 与正在克隆的对象共享状态。如果这个状态是可变的, 没有两个独立的对象。如果修改其中一个,则另一个 变化也是如此。突然间,你会有随机行为

不变的


一个更好的模式是使一切都不可变。这样,您就不需要单独的实例,您可以共享实例,直到它们需要更改,然后它们更改,您就有了一个包含新数据的新实例,可以在没有任何副作用的情况下共享。

谢谢,Edalorzo!!我听从了你的建议,效果很好!!我继续把代码扩展了一点。。。基本上,我明确指定了字段,再次感谢您的领导!!'this.portfolio=新的ArrayList;forInstrument sourceInstrumentNow:copyFrom.getPortfolio{Instrument targetInstrumentNow=new Instrument;targetInstrumentNow.SetBBG SourceInstrumentNow.getBBG;targetInstrumentNow.setCCYsourceInstrumentNow.getCCY;targetInstrumentNow.setBaseCurrencysourceInstrumentNow.getBaseCurrency;targetInstrumentNow.setContractMultipliersourceInstrumentNow.getContractMultiplier;targetInstrumentNow.setDeltasourceInstrumentNow.getDelta;'`targetInstrumentNow.setDescriptionsourceInstrumentNow.getDescription;targetInstrumentNow.setFXratesourceInstrumentNow.getFXrate;targetInstrumentNow.setInvestmentTypesourceInstrumentNow.getInvestmentType;targetInstrumentNow.setMV_basesourceInstrumentNow.getMV_base;targetInstrumentNow.setMV_localsourceInstrumentNow.getMV_local;targetInstrumentNow.setModelsourceInstrumentNow.getModel;targetInstrumentNow.setPositionTypesourceInstrumentNow.getPositionType;`targetInstrumentNow.setPrice SourceInstrumentNow.getPrice;targetInstrumentNow.setQuantitysourceInstrumentNow.getQuantity;targetInstrumentNow.setSecurityIDsourceInstrumentNow.getSecurityID;targetInstrumentNow.SetUnderlineingSourceInstrumentNow.GetUnderlined;this.portfolio.addtargetInstrumentNow;targetInstrumentNow=null;@user1424880如果此工作为或者你可以选择它作为问题的答案。谢谢,Edalorzo!!我遵循了你的建议,效果很好!!我继续并扩展了一点代码…本质上,我再次明确指定了字段非常感谢您的领导!!'this.portfolio=new arrarylist;forInstrument sourceInstrumentNow:copyFrom.getPortfolio{Instrument targetInstrumentNow=new Instrument;targetInstrumentNow.SetBBG SourceInstrumentNow.getBBG;targetInstrumentNow.setCCYsourceInstrumentNow.getCCY;targetInstrumentNow.setBaseCurrencysourceInstrumentNow.getBaseCurrency;targetInstrumentNow.setContractMultipliersourceInstrumentNow.getContractMultiplier;targetInstrumentNow.setDeltasourceInstrumentNow.getDelta;'`targetInstrumentNow.setDescriptionsourceInstrumentNow.getDescription;targetInstrumentNow.setFXratesourceInstrumentNow.getFXrate;targetInstrumentNow.setInvestmentTypesourceInstrumentNow.getInvestmentType;targetInstrumentNow.setMV_basesourceInstrumentNow.getMV_base;targetInstrumentNow.setMV_localsourceInstrumentNow.getMV_local;targetInstrumentNow.setModelsourceInstrumentNow.getModel;targetInstrumentNow.setPositionTypesourceInstrumentNow.getPositionType;`targetInstrumentNow.setPrice SourceInstrumentNow.getPrice;targetInstrumentNow.setQuantitysourceInstrumentNow.getQuantity;targetInstrumentNow.setSecurityIDsourceInstrumentNow.getSecurityID;targetInstrumentNow.SetUnderlineingSourceInstrumentNow.GetUnderlined;this.portfolio.addtargetInstrumentNow;targetInstrumentNow=null;@user1424880如果此工作为或者你可以选择它作为问题的答案。Jarrod,非常感谢你提供的详细答案!!我感谢你的帮助!是的,我没有使用.clone方法的丰富经验,这确实让人大开眼界。谢谢你链接到Josh Bloch的页面-信息量非常大。我结合了你和edalorzo的建议,现在又有了新的内容我们的工作很有魅力。再次感谢!!Jarrod,非常感谢您提供的详细答案!!我感谢您的帮助!是的,我没有使用.clone方法的丰富经验,这确实让人大开眼界。感谢链接到Josh Bloch的页面-信息非常丰富。我结合了您和edalorzo的建议,现在一切都很有魅力比 又来了!!
Portfolio pCopy = new Portfolio(p);
this.portfolio = new ArrayList<Instrument>();
for(Instrument toBeCopiedInstrument : copyFrom.portfolio){
   this.portfolio.add(new Instrument(toBeCopiedInstrument ));
}