我有一个来自不同类的对象的 vector 列表,我需要在其中调用特定于类的方法。这是一个例子
这里是对象的类,
public class VariableElement extends FormulaElement{
private double varValue;
public void setVariableValue(double varValue) {
this.varValue = varValue;
}
}
这里是我要调用该方法的地方,这是 FormulaElement 类中的另一个方法
public void setVariableValue(double value){
for(Object o:tokenVector){
if(o instanceof VariableElement){
o.setVariableValue(value);//throws error symbol not found
}
}
}
这基本上是我想做的,但它给出了一个错误,我该如何解决这个问题,有可能吗?预先感谢:)
最佳答案
首先将您的对象转换为 VariableElement。
public void setVariableValue(double value){
for(Object o:tokenVector){
if(o instanceof VariableElement){
VariableElement ve = (VariableElement)o;
ve.setVariableValue(value);
}
}
}
关于java - 如何调用存储在包含各种对象的 vector 中的对象的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56940024/