我处于一个我已经超越语法错误的阶段,所以我发现为什么有时为什么我的代码行为不正确而且这是其中一个时间并且非常喜欢你的指导。
代码段如下:
@Override
public void run() {
for (int i = 0; i < eventStatus.length; i++)
eventStatus[i] = new EventStatus();
Random randomGenerator = new Random();
boolean proceed = false;
boolean finished = false;
while (!finished) {
//System.out.println(eventImpl.getEventType());
if (("INITIAL".equals(eventImpl.getEventType())) && (!eventStatus[0].isHasOccured())) {
eventStatus[0].setHasOccured(true);
eventStatus[0].setHasSent(true);
eventStatus[0].setEventImpl(eventImpl);
System.out.println("one");
consumeEvent(eventStatus[0].getEventImpl());
if (eventStatus[1].isHasOccured()) {
System.out.println("two");
eventStatus[1].setHasSent(true);
consumeEvent(eventStatus[1].getEventImpl());
proceed = true;
} else {
proceed = false;
}
if ((eventStatus[2].isHasOccured()) && proceed) {
System.out.println("three");
eventStatus[2].setHasSent(true);
consumeEvent(eventStatus[2].getEventImpl());
proceed = true;
} else {
proceed = false;
}
if ((eventStatus[3].isHasOccured()) && proceed) {
System.out.println("four");
eventStatus[3].setHasSent(true);
consumeEvent(eventStatus[3].getEventImpl());
proceed = true;
} else {
proceed = false;
}
if ((eventStatus[4].isHasOccured()) && proceed) {
System.out.println("five");
eventStatus[4].setHasSent(true);
consumeEvent(eventStatus[4].getEventImpl());
proceed = true;
} else {
proceed = false;
}
System.out.println("INITIAL Here");
}
if (("CREATING".equals(eventImpl.getEventType())) && (!eventStatus[1].isHasOccured())) {
eventStatus[1].setHasOccured(true);
eventStatus[1].setEventImpl(eventImpl);
System.out.println("CREATING Here");
}
if (("CREATED".equals(eventImpl.getEventType())) && (!eventStatus[2].isHasOccured())) {
eventStatus[2].setHasOccured(true);
eventStatus[2].setEventImpl(eventImpl);
System.out.println("CREATED Here");
}
if (("CLOSING".equals(eventImpl.getEventType())) && (!eventStatus[3].isHasOccured())) {
eventStatus[3].setHasOccured(true);
eventStatus[3].setEventImpl(eventImpl);
System.out.println("CLOSING Here");
}
if (("CLOSED".equals(eventImpl.getEventType())) && (!eventStatus[4].isHasOccured())) {
eventStatus[4].setHasOccured(true);
eventStatus[4].setEventImpl(eventImpl);
System.out.println("CLOSED Here");
}
if (!finished) {
eventImpl.setEventType(EventImpl.eventTypes.values()[randomGenerator.nextInt(5)]);
}
}
}
辅助类:
public class EventStatus {
private boolean hasOccured = false;
private boolean hasSent = false;
private EventImpl eventImpl;
public boolean isHasOccured() {
return hasOccured;
}
public boolean isHasSent() {
return hasSent;
}
public void setHasOccured(boolean hasOccured) {
this.hasOccured = hasOccured;
}
public void setHasSent(boolean hasSent) {
this.hasSent = hasSent;
}
public EventImpl getEventImpl() {
return eventImpl;
}
public void setEventImpl(EventImpl eventImpl) {
this.eventImpl = eventImpl;
}
}
当我试图为每个&#39;当我运行代码时存储EventImpl类的实例时,问题是存储的对象不运行,而是&#39; if&的默认值#39;对象满足每个条件执行。
例如:
CLOSED Here
CREATED Here
CREATING Here
CLOSING Here
one
Event ID: 7573f0fe-d848-4049-8d5c-28fcbf5c8497 Event: INITIAL
two
Event ID: 7573f0fe-d848-4049-8d5c-28fcbf5c8497 Event: INITIAL
three
Event ID: 7573f0fe-d848-4049-8d5c-28fcbf5c8497 Event: INITIAL
four
Event ID: 7573f0fe-d848-4049-8d5c-28fcbf5c8497 Event: INITIAL
five
Event ID: 7573f0fe-d848-4049-8d5c-28fcbf5c8497 Event: INITIAL
INITIAL Here
除了一个INITIAL之外,每个INITIAL都应该说明先前存储在我的助手类中的对象。
那么请你告诉我如何完成这件事呢?
@Override
public synchronized void consumeEvent (Event theEvent) {
System.out.println("Event ID: " + theEvent.getEventId() + " Event: " + theEvent.getEventType());
}
答案 0 :(得分:0)
您只涉及一个Event
(EventImpl
)对象 - 包含这些方法的类的eventImpl
实例变量中存储的任何内容。在循环的每次迭代中,将对象的引用分配给eventStatus
数组的一个元素的属性,并将同一对象的eventType
属性设置为随机值,但都不是action创建一个新对象。
在最终将类型设置为“INITIAL”之后,程序会遍历并使用存储在EventImpl
数组的一个或多个元素中的eventStatus
,但包含一个元素的每个元素都包含相同的。
我很难说出你在这里想要做什么,或者为什么要像你一样接近它,但程序报告的行为正如它的代码引导我期待的那样。