-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathBeanShell04.bsh
73 lines (60 loc) · 2.15 KB
/
BeanShell04.bsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* BeanShell04.bsh
* IJ BAR: https://github.com/tferr/Scripts#scripts
* *************************************************
* 4. Methods and the super keyword
* *************************************************
*/
// BeanShell methods ("Java functions") are defined as
// in Java, with return values specified by a <return>
// statement. Bsh methods allow dynamic (loose) of both
// arguments and return types. We will come back to this
// in a second. Meanwhile, here is an example of a
// method that takes no arguments:
/** This method returns a string of today's date */
today() {
import java.util.Date; // http://javadoc.imagej.net/Java7/index.html?java/util/Date.html
date = new Date();
return date.toString(); // http://javadoc.imagej.net/Java7/index.html?java/lang/Object.html#toString--
}
print("Line 23: today() returned" + today());
// A Method with arguments:
/**
* This method returns the smallest of two arguments, and is a
* kludged assembly of Java.lang.Math.min(), see
* http://javadoc.imagej.net/Java7/index.html?java/lang/Math.html
*/
minMethod(a, b) {
if (a < b) {
return a;
} else {
return b;
}
}
print("Line 40: " + minMethod(10,20) + "is smaller");
// We can also specify argument and/or return types:
/** This method sums only integers */
int intSum(int a, int b) {
return a + b;
}
print("Line 49: The sum of 2 integers: " + intSum(2,4) );
// void methods do not return any value:
void voidMethod() {
print("I return nothing. I just print stuff.");
}
print("Line 55: " + voidMethod());
// Now let's have a look at "Global variables":
// It is possible to explicitly refer to a declared variable
// (or method) defined outside the scope of a method using
// the <super> keyword, similarly to Python's <global>
// keyword (Python04.py tutorial). Example:
int variable = 2;
myMethod() {
int variable = 40;
print("Line 66: Inside the method, variable= " + variable);
print("Line 67: Outside the method, variable= " + super.variable);
// Note that this would not work for loosely declared variables
// What happens when one does not specify the variable data type?
}
myMethod();
// To know more: http://www.beanshell.org/manual/