简体   繁体   English

在 Anylogic 中复制不带 inheritance 的整个 javaclass 或其参数

[英]Copy entire javaclass or its parameters without inheritance in Anylogic

I'm trying to model a factory with production orders in Anylogic.我正在尝试 model 一家在 Anylogic 有生产订单的工厂。 Unfortunately, I'm a beginner in java...不幸的是,我是 java 的初学者......

I created two new JavaClasses for my model.我为我的 model 创建了两个新的 JavaClass。 A "bill of material"(stueckliste) and "entry of bill of material"(stuecklisteneintrag).一份“材料清单”(stueckliste) 和“材料清单条目”(stuecklisteneintrag)。 The bill of material has a linked list:物料清单有一个链表:

String name;
LinkedList<Stuecklisteneintrag>stuecklisteneintrag = new LinkedList<Stuecklisteneintrag>();

The "entry of bill of material"(stuecklisteneintrag) class has a lot of parameters. “物料清单条目”(stuecklisteneintrag) class 有很多参数。

If i want to create a如果我想创建一个

new Stueckliste();

with new entries:新条目:

Stuecklisteneintrag stkE = new Stuecklisteneintrag() Stuecklisteneintrag stkE = new Stuecklisteneintrag()

based on existing entries:基于现有条目:

stkE = stueckliste.stuecklisteneintrag.get(test);

How do I copy an entire "entry" without the inheritance to the existing entry?如何将没有 inheritance 的整个“条目”复制到现有条目? Because if i change the parameters, both parameters in the old and new entry will change...因为如果我更改参数,旧条目和新条目中的两个参数都会改变......

stkE = stueckliste.stuecklisteneintrag.get(test);
a.fertigungsstueckliste.stuecklisteneintrag.add(stkE);
a.fertigungsstueckliste.stuecklisteneintrag.getFirst().name= "Test";
traceln(a.fertigungsstueckliste.stuecklisteneintrag.getFirst().name);
traceln(stueckliste.stuecklisteneintrag.getFirst().name);

Both names will be "test".这两个名字都是“test”。 Or if i delete entries in the new bill of material, entrys in the old one are deleted as well...或者,如果我删除新物料清单中的条目,旧物料清单中的条目也会被删除......

You need to do a "deep copy" of your data.您需要对数据进行“深拷贝”。 Create a new constructor for Stuecklisteneintrag as:Stuecklisteneintrag创建一个新的构造函数:

public Stuecklisteneintrag(Stuecklisteneintrag oldEintrag) {
    this.field1 = oldEintrag.field1;
    ... // add all fields here
}

This will create a new Stuecklisteneintrag instance that takes from the oldEintrag instance but they are now "de-coupled":)这将创建一个新的Stuecklisteneintrag实例,该实例取自oldEintrag实例,但它们现在“解耦”:)

NOTE: You can only do this for primitive data types (int, double, boolean) as well as for String s.注意:您只能对primitive数据类型(int、double、boolean)以及String s 执行此操作。 If you have fields that are other Java classes, they need deep copies as well!如果您有其他 Java 类的字段,它们也需要深拷贝!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM