beanshell inner class
问题描述:
我想使用我的java代码作为beanshell脚本,但beanshell抛出异常,说在命名空间中找不到类。 beanshell中没有内部类还是有其他用法?beanshell inner class
我的脚本看起来像在这里:
.......
.......
java code
.......
.......
MyClass m = new MyClass(); //Error here: MyClass not fount in namespace
class MyClass {
}
我使用脚本内部类,这是我在脚本中声明。
感谢, 比拉尔
答
也许一个愚蠢的在这里,但答案会不会是myClass定义必须上述它在文件中使用?不是线性地bean shell处理脚本?
就让我们来看看在这个文档中并没有清楚这一点,但下面的脚本的测试肯定能正常工作对我来说:
+0
它没有工作。我发现了另一种不使用内部类的方法。但是这是2.我逃离内部阶层的时候。我读了很多次,beanshell接受内部类,但它从来没有为我工作。 – bilal 2010-09-23 09:10:10
答
类定义不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);
希望这将有助于出人。
Bilal,看到你的代码会很有帮助,以便找出它的问题。 BTW Beanshell也支持内部类和(自v2.0以来)匿名内部类。 – 2010-09-10 12:38:50