我有2个类,它们实现了名为IProcess的相同接口。
IProcess.java
package org.XXX.module.workflow.bo.interfaces;
public interface IProcess extends IMasterObject {
INode getCurrentNode();
INode getPreviousNode();
long getLastChange();
IWorkflow getWorkflow();
}
头等舱
package org.XXX.module.workflow.database.bo.impl;
import org.XXX.module.workflow.bo.interfaces.INode;
import org.XXX.module.workflow.bo.interfaces.IProcess;
import org.XXX.module.workflow.bo.interfaces.IWorkflow;
import org.XXX.module.workflow.database.jdbc.entity.ProcessEntity;
public class ProcessEntityWrapper implements IProcess {
private String id;
private INode currentNode;
private INode previousNode;
private long lastChange;
private IWorkflow workflow;
public ProcessEntityWrapper(ProcessEntity entity) {
this.id = entity.getId();
this.lastChange = entity.getLastChange();
}
@Override
public String getId() {
return id;
}
@Override
public INode getCurrentNode() {
return currentNode;
}
@Override
public INode getPreviousNode() {
return previousNode;
}
@Override
public long getLastChange() {
return lastChange;
}
@Override
public IWorkflow getWorkflow() {
return workflow;
}
/**
* @param currentNode the currentNode to set
*/
public void setCurrentNode(INode currentNode) {
this.currentNode = currentNode;
}
/**
* @param prevoiusNode the prevoiusNode to set
*/
public void setPreviousNode(INode previousNode) {
this.previousNode = previousNode;
}
/**
* @param workflow the workflow to set
*/
public void setWorkflow(IWorkflow workflow) {
this.workflow = workflow;
}
}
第二课:
package org.XXX.module.workflow.bo.impl;
import org.XXX.module.workflow.bo.interfaces.INode;
import org.XXX.module.workflow.bo.interfaces.IProcess;
import org.XXX.module.workflow.bo.interfaces.IWorkflow;
public class Process implements IProcess {
public Process(String id, INode currentNode, INode previousNode,
long lastChange, IWorkflow workflow) {
this.id = id;
this.currentNode = currentNode;
this.previousNode = previousNode;
this.lastChange = lastChange;
this.workflow = workflow;
}
private String id;
private INode currentNode;
private INode previousNode;
private long lastChange;
private IWorkflow workflow;
@Override
public String getId() {
return id;
}
@Override
public INode getCurrentNode() {
return currentNode;
}
@Override
public INode getPreviousNode() {
return previousNode;
}
@Override
public long getLastChange() {
return lastChange;
}
@Override
public IWorkflow getWorkflow() {
return workflow;
}
/**
* @param currentNode the currentNode to set
*/
public void setCurrentNode(INode currentNode) {
this.currentNode = currentNode;
}
/**
* @param previousNode the previousNode to set
*/
public void setPreviousNode(INode previousNode) {
this.previousNode = previousNode;
}
/**
* @param lastChange the lastChange to set
*/
public void setLastChange(long lastChange) {
this.lastChange = lastChange;
}
}
Part,抛出ClassCastException:
@Override
public void route(IProcess process, INode nextNode) {
if(initializedClient == null) {
return;
}
Process processImpl = (Process) process;
processImpl.setPreviousNode(process.getCurrentNode());
processImpl.setCurrentNode(nextNode);
dataAccess.updateProcess(processImpl);
}
流程是ProcessEntityWrapper -Object, processImpl 是流程对象
我不知道,为什么特定代码会抛出 ClassCastException 。请问有人帮帮我吗?
由于
修改
@Override
public void updateProcess(IProcess process) {
TypedQuery<ProcessEntity> q = em.createQuery(WorkflowAccessProperties.SELECT_PROCESSES_BY_ID,ProcessEntity.class);
q.setParameter("id", process.getId());
ProcessEntity entity = (ProcessEntity) q.getSingleResult();
em.getTransaction().begin();
entity.setLastChange(System.currentTimeMillis());
String previousId = null;
if(process.getPreviousNode() != null) {
previousId = process.getPreviousNode().getId();
}
entity.setPreviousNodeId(previousId);
entity.setCurrentNodeId(process.getCurrentNode().getId());
em.getTransaction().commit();
}
答案 0 :(得分:2)
虽然ProcessEntityWrapper
和Process
实现了相同的界面,但它们并没有相互扩展。
它们是实现相同接口的不同类。
任何类都可以实现接口。这并不意味着它们可以转换为彼此。
示例:牛和鸟可以实现相同的界面,但是你不能仅仅因为它们实现相同的界面而将牛转换为鸟。
public interface Speakable(){
public String speak();
}
public class Cow implements Speakable(){
public String speak(){
return "moooo";
}
}
public class Bird implements Speakable(){
public String speak(){
return "vik vik";
}
}