我想使用我的java代码作为beanshell脚本,但是beanshell抛出异常,说在命名空间中找不到class。beanshell中没有内部类吗?或者它有其他用途吗?
我的脚本如下所示:
.......
.......
java code
.......
.......
MyClass m = new MyClass(); //Error here: MyClass not fount in namespace
class MyClass {
}
我在脚本中使用内部类,我在脚本中声明了它。
谢谢,比拉尔
发布于 2010-09-21 11:19:59
也许这是一个愚蠢的答案,但会不会是MyClass定义需要高于它在文件中的用法呢?bean shell进程脚本不是线性的吗?
快速浏览一下文档并不能说明这一点,但对以下脚本的测试对我来说肯定很好:
class MyClass {
}
MyClass m = new MyClass();
发布于 2013-01-06 23:07:23
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
发布于 2016-09-14 21:29:54
附加信息:匿名内部类作为参数不能使用,因此您需要将实现赋给一个变量。(在JMeter中)
不起作用:
object.setContext(new SomeInterface(){
//implement methods
});
作品:
SomeInterface ctx = new SomeInterface(){
//implement methods
});
object.setContext(ctx);
希望它能帮助到某个人。
https://stackoverflow.com/questions/3683562
复制相似问题