为了工作,我必须使用一个不同公司的图书馆来与一个管理员进行通信。这就是bcomponent的来源。它的文档记录很差,但我必须使用它。作为一个例子,我创建了一个类(bihcstemplate),其中实现了一些基本的东西。为了减少代码并提高可读性,我创建了triggeraction方法。这很管用。
这是可行的(bihcstemplate.java)
public class BIhcsTemplate extends BComponent implements Runnable {
//Main action this is the main loop that executes the code
public static final Action execute = newAction(Flags.ASYNC, null);
public void execute() {invoke(execute, null, null);}
//Autogenerated calls for exampleAction
public static final Action ExampleAction = newAction(Flags.HIDDEN, null);
public void ExampleAction() {invoke(ExampleAction, null, null);}
//this gets and set a boolean
public static final Property bExample = newProperty(Flags.EXECUTE_ON_CHANGE | Flags.OPERATOR | Flags.SUMMARY, ((BBoolean) ((BValue) BBoolean.TYPE.getInstance())).getBoolean(), BFacets.tryMake(null));
public boolean getBExample() {return getBoolean(bExample);}
public void setBExample(boolean v) {setBoolean(bExample, v, null);}
public BComponent getComponent() {return this;}
public BComponent getProgram() {return this;}
//main (this runs the code)
public void onExecute() throws Exception {
if(getBExample()){
//This works
triggerAction("ExampleAction");}
}
//created method to reduce writing the same line/code over and over
public void triggerAction(String nameOfAction) {
try {
this.getComponent().invoke(this.getComponent().getAction(nameOfAction), (BValue) null); //calls public void exampleAction
} catch (Exception e) {
//TODO maybe always write to generic debug file?
System.out.println(e);
}
}
}
我现在想做的。将triggeraction方法放在另一个类(libraryclass.java)中并从那里调用它。这样地:
public class LibraryClass extends BComponent {
public BComponent getComponent() {return this; }
public BComponent getProgram() {return this;}
public void triggerAction(String nameOfAction) {
try {
this.getComponent().invoke(this.getComponent().getAction(nameOfAction), (BValue) null);
} catch (Exception e) {
//TODO maybe always write to generic debug file?
System.out.println(e);
}}
}
现在使用bihcstemplateclass中libraryclass的方法:
//this now extends library class to use its method
public class BIhcsTemplate extends LibraryClass implements Runnable {
//Main action this is the main loop that executes the code
public static final Action execute = newAction(Flags.ASYNC, null);
public void execute() {invoke(execute, null, null);}
//Autogenerated calls for exampleAction
public static final Action ExampleAction = newAction(Flags.HIDDEN, null);
public void ExampleAction() {invoke(ExampleAction, null, null);}
//this gets and set a boolean
public static final Property bExample = newProperty(Flags.EXECUTE_ON_CHANGE | Flags.OPERATOR | Flags.SUMMARY, ((BBoolean) ((BValue) BBoolean.TYPE.getInstance())).getBoolean(), BFacets.tryMake(null));
public boolean getBExample() {return getBoolean(bExample);}
public void setBExample(boolean v) {setBoolean(bExample, v, null);}
public BComponent getComponent() {return this;}
public BComponent getProgram() {return this;}
//main (this runs the code)
public void onExecute() throws Exception {
if(getBExample()){
triggerAction("ExampleAction");}
}}}
上述示例不起作用/无法触发新操作。我相信这是因为getcomponent()是另一个示例/在调用triggeraction时在另一个类中处理。
我怎样才能做到这一点,有没有可能做到这一点?任何帮助都将不胜感激!
我尽量说清楚(我是java新手)如果有任何问题请告诉我。
暂无答案!
目前还没有任何答案,快来回答吧!