我有这个用java
编写的相当简单的代码。这实际上来自DAQ框架,称为Kmax
import kmax.ext.*;
public class Runtime implements KmaxRuntime {
KmaxToolsheet tlsh; // Store a reference to the toolsheet environment
KmaxHist hist1D;
KmaxWidget checkBoxWidget;
public void init(KmaxToolsheet toolsheet) {
tlsh = toolsheet; // Save this reference for use in the toolsheet
hist1D = tlsh.getKmaxHist("HIST1D");
checkBoxWidget = tlsh.getKmaxWidget("CHECK_BOX_CALIB_METH");
tlsh.getKmaxWidget("CHECK_BOX_CALIB_METH").setProperty("VALUE", "1");
}
public void CalibInit(KmaxWidget widget, KmaxHist histo){
histo.setUseXAxisCalibration(stringToBool(widget.getProperty("VALUE")));
histo.update();
}
CalibInit(checkboxWidget,hist1D);
public void GO(KmaxToolsheet toolsheet){}
public void SRQ(KmaxDevice device) {}
public void HALT(KmaxToolsheet toolsheet) {}
} // End of the Runtime object
请注意,我创建了一个名为CHECK_BOX_CALIB_METH
的对象。当我编译这段代码时,我得到了那些错误消息
compiler msg>error: invalid method declaration; return type required
compiler msg> CalibInit(checkboxWidget,hist1D);
compiler msg> ^
compiler msg>error: <identifier> expected
compiler msg>CalibInit(checkboxWidget,hist1D);
compiler msg> ^
compiler msg>error: <identifier> expected
compiler msg>CalibInit(checkboxWidget,hist1D);
compiler msg> ^
请注意,如果我删除CalibInit
方法并将其替换为
public void CHECK_BOX_CALIB_METH(KmaxWidget widget) {
hist1D.setUseXAxisCalibration(stringToBool(widget.getProperty("VALUE")));
hist1D.update();
}
我没有编译错误。关键点是方法的名称与对象的名称相同。我创建CalibInit()
的原因是为了避免为每个具有相同功能的相同类型的对象使用每个方法。有办法吗?
如何避免这些错误?
答案 0 :(得分:2)
只有变量可以声明方法的一面。您只能在方法和构造函数中调用方法(在此处避免使用静态上下文)。
CalibInit(checkboxWidget,hist1D);
如果需要,请将该行移动到任何方法或构造函数。更具体地说,在您需要的地方打电话
简而言之: CalibInit(checkboxWidget,hist1D);
现在是孤儿。使它属于某种东西。
答案 1 :(得分:1)
代码
CalibInit(checkboxWidget,hist1D);
在它自己的一行上不在你的任何方法中。编译器假定这是一个新的方法声明,可能不是你想要的。
旁注:
不建议使用以大写字母开头的方法:&#34;方法应该是动词,大小写混合,第一个字母小写,每个内部单词的首字母大写。&#34;来自Code Conventions for the Java Programming Language
答案 2 :(得分:1)
你不能打电话
CalibInit(checkboxWidget,hist1D);
直接在课堂上,就像你正在做的那样。如果您的目标是在构造运行时实例时调用它,则此指令应位于构造函数内。
BTW:方法在Java中按照惯例以小写字母开头,你不应该调用你的类Runtime:它会让人感到困惑,因为标准库中已经存在标准的Runtime类。
答案 3 :(得分:1)
您正在类中直接调用CalibInit(checkboxWidget,hist1D)方法,而不是在任何方法中。 Java不支持这一点。