Groovy User Guide (1)

xiaoxiao2021-03-06  40

1, class

(1) class

l Groovy's class definition and Java similar

Ø Method can be based on class (STATIC) or instance

Ø can be public, protected or private

Ø Support common Java modifiers, such as Synchronized

l Groovy's different places: By default is public

l Groovy supports JavaBean mechanism: Groovybean

l Each Groovy class is a Java class with a zodical code / JVM. Any method is valid for Java, and vice versa.

l You can specify the parameter type and return type of the method, so you can work better in the usual Java code.

l You can use the above way to implement an interface or overload method

l If the type of method is omitted, the default java.lang.object will be used.

(2) script

l Groovy supports pure script without a statement, such as foo.groovy contains the following script:

Println "Nice Cheese Gromit!"

l The process of running scripts:

Ø Compile into foo.class (there will be some inside into the category), this class extension Groovy.lang.Script

Ø Execute the automatically generated main method

Ø Instantiate FOO class

Ø Call the RUN method to perform scripting content

l You can execute a script in the Java code, and you can also transfer variables to the script.

l foo.groovy content is modified as follows

Println "Nice $ {Cheese} GROTI!"

l The following is the USEFOO class that performs scripts.

Import groovy.lang.binding;

Import groovy.lang.script;

Public class usefoo {

Public static void main (String [] args) {

Binding binding = new binding ();

Binding.Setvariable ("Cheese", "ChedDar");

Script foo = new foo (binding);

Foo.Run ();

}

}

l Usefoo running results: Nice Cheddar Gromit!

l The method of executing the script is to create a Foo class instance and call its RUN method.

l Foo class has a constructor with a binding parameter, which can set the value to the attribute variable in the script via the Binding class's setvariable method.

l Foo class has a constructor without parameters, uses when not transferring attribute variables

l After the script is over, any variable created in the script will be in Binding for access in Java.

l Remove the foo.groovy content as follows

Println "Nice $ {Cheese} GROTI!"

Cheese = "Changed"

l Usefoo class modifications to:

Import groovy.lang.binding;

Import groovy.lang.script;

Public class usefoo {

Public static void main (String [] args) {

Binding binding = new binding (); binding.setvariable ("cheese", "cheddar");

Script foo = new foo (binding);

Foo.Run ();

Println Binding.getvariable ("Cheese");

}

}

l Usefoo runs the result:

Nice Cheddar Gromit!

changed

(3) Functions in scripts

l Different from the Class-based Groovy, the function in the pure script is declared by DEF keyword

DEF foo (list, value) {

Println "Calling function foo () with param $ {value}

List << Value

}

X = []

Foo (x, 1)

Foo (x, 2)

askERT X == [1, 2]

Println "Creating List $ {x}"

转载请注明原文地址:https://www.9cbs.com/read-116334.html

New Post(0)