题
我想将我的Java代码用作Beanshell脚本,但Beanshell引发了异常,说明了名称空间中找不到的类。 Beanshell中是否有内在类别,或者还有其他用法吗?
我的脚本看起来像:
.......
.......
java code
.......
.......
MyClass m = new MyClass(); //Error here: MyClass not fount in namespace
class MyClass {
}
我在脚本中使用脚本中的内部类。
谢谢,比拉尔
解决方案
也许这里是一个愚蠢的答案,但可能是myclass的定义需要 多于 它在文件中的用法? Bean shell处理脚本不会线性吗?
快速查看文档并没有说明这一点,但是以下脚本的测试对我来说肯定不错:
class MyClass {
}
MyClass m = new MyClass();
其他提示
Beanshell不支持类定义。
您可以使用Beanshell内部类语法来实现接口:
x = new MyInterface() {
overriddenMethod() {
// ....
}
}
v = x.overriddenMethod();
或者
overriddenMethod() {
//.....
}
// 'this' is a object of whatever Foo expects
//
new Foo(this);
就您而言,我认为您最好使用脚本对象方法:
myClass() {
// methods ...
return this;
};
m = myClass(); // new instance
其他信息: 匿名内部类 作为参数 无法使用,因此您需要将实现分配给变量。 (在Jmeter中)
无效:
object.setContext(new SomeInterface(){
//implement methods
});
作品:
SomeInterface ctx = new SomeInterface(){
//implement methods
});
object.setContext(ctx);
希望它能帮助某人。
不隶属于 StackOverflow