beanshell学习笔记(二)---基本语法:scripted method andObject

Scripting Method
在beanshell中方法的修饰关键字只有synchronized,在方法上用throws关键字会检查此方法的名字的正确性,不过声明throws不是必须的
一个对象的方法被同步意味着这个方法普遍的作用范围,beanshell中同步方法的作用和java中同步方法的作用差不多
当方法内的变量重写(覆盖)了方法外面的变量时,在beanshell中调用作用域外面的变量用关键字super,类如:
int a = 42;

foo() {
    int a = 97;
    print( a );
    print( super.a );
}

foo();  // prints 97, 42



Scripting Object
在java中如若有一下定义:
 // MyClass.java
    MyClass {
        Object getObject() {
            return this; // return a reference to our object
        }
    }

则可以通过如上所示的this关键字来返回实例化方法后的MyClass对象的实例.在beanshell中this可以代表返回方法的本体对象,如:
引用
foo() {
    int bar = 42;
    return this;
}

fooObject = foo();
print( fooObject.bar ); // prints 42!


在beanshell中还可以在方法里面嵌套方法,你可以把每个方法看成一个对象:
foo() {
    int a = 42;
    bar() {
        print("The bar is open!");
    }
    
    bar();
    return this;
}

// 构造foo对象
fooObject = foo();     // pints "the bar is open!"
// Print a variable of the foo object
print ( fooObject.a );  // 42
// Invoke a method on the foo object
fooObject.bar();       // prints "the bar is open!"


甚至还可以多层嵌套.只要你能分得清:
foo() {

	bar() { }

	if ( true ) {
		bar2() { }
	}

	return this;
}




你可能感兴趣的:(java)