1. From the example
a) Objective: Demonstrate some simple features of Groovy.
b) code:
Class gtest1 {
Gtest1 ()
Hello (Name) {
Println ("Hello $ {Name}");
Println ("Hello" Name);
}
Static void main (args) {
T = New gtest1 ();
T. Hello ("Nick");
}
}
c) Output:
Hello Nick
Hello Nick
d) Description:
I. No need to declare the variable type. Since Groovy uses dynamic discovery types, this is a bit similar to JavaScript, so we don't need to explicitly declare our variable type, and his type will be determined at runtime.
II. No need to declare access control (public, private ...). In Groovy, the default access control is public, so when we do not explicitly declare, their access control is public.
Iii. Method does not need to declare return value. In Groovy, the return value of the method is Object.
IV. Variable value reference. In the Hello method, we call the PrintLn method, we can use the traditional Java's pass " " to connect parameters, or the $ {variable name} method provided by new, Groovy.
v. The end of the expression can not be a semicolon. Groovy allows the programmer to add a semicolon at the end of the expression.
2. Class definition
a) Objective: Demonstrate how to define and initialize the class in Groovy.
b) code:
Class gtest2 {
Name;
Job;
Hello (Name, Job) {
Println ("Hello $ {Name} $ {JOB}");
Println ("Hello $ {this.name} $ {this.job}");
}
Static void main (args) {
T1 = new gtest2 (Name: "cen", Job: "IT")
T1.Hello ("Nick", "Student");
T2 = new gtest2 (Job: "IT", Name: "cen")
T2.Hello ("Nick", "Student");
}
}
c) Output:
Hello Nick Student
Hello Cen IT
Hello Nick Student
Hello Cen IT
d) Description:
i. Property value declaration. Since Groovy does not need to explicitly declare access controls and variable types, it is only necessary to provide a variable name when we define attributes Name and Job for gtest2.
Ii. Constructor. When declare, we don't explicitly provide constructor, and Groovy will provide us with all the available constructor according to all declarative properties, and when we need to initialize a class, we need to explicitly indicate which construct we call us. function. It is especially interesting here that in Java, because the method only distinguishes only by the method name and parameter list, the String Name, String Jobs and GTEST2 (String Job) are not possible, but Among Groovy, Gteest2 (Name: "Cen", Job: "IT") and GTEST2 (Job: "IT", name: "cen") are valid. Iii. THIS reference. When the method's parameter name and class attribute name are the same, we can use this reference to indicate the values we reference.
3. Access
a) Objective: Demonstrate how to call the class's properties, class methods, and instance methods.
b) code:
Class gtest3 {
Name;
Hello (Name) {
Println "Hello $ {Name} $ {this.name}";
}
Static Hi (Name) {
Println "Hi $ {Name}"
}
Static void main (args) {
Gtest3.hi ("Nick");
T = New GTEST3 (Name: "cen");
T.hello ("a");
Println T.Name;
T.Name = "cencen";
Println T.Name;
}
}
c) Output:
Hi Nick
Hello a cen
cen
CenCen
d) Description:
i. Property Access. Since the default access control is public, we do not need to access and modify the properties of classes directly through the Get / SET method.
4. Class inheritance and interface implementation
a) Objective: Demonstrate how to make a class in Groovy, implementation with the interface.
b) code:
Interface code:
Public interface gtestinter {
Public void Bye (String Name);
}
Class code:
Class Gtest4 Extends Gtest3 Implements Gtestinter {
BYE (Name) {
Println ("BYE $ {Name}")
}
Static void main (args) {
T = New gtest4 (Name: "Nick")
T.hello ("a")
T.BYE ("B")
}
}
c) Output:
Hello a Nick
Bye B
d) Description:
i. Interface definition. Direct definition interfaces are not supported in Groovy.
II. Interface implementation. Groovy allows the Groovy class to implement a well-defined interface in Java in a general manner.
Iii. Class integration. In Groovy, class inheritance is consistent with Java.
5. Closure
a) Objective: Closure is a powerful mechanism for Groovy, which is used here. b) code:
Class gtest5 {
Name;
Job;
Static void main (args) {
T = new gtest5 (Name: "Nick", Job: "IT");
T. EachPropertyName ({Println ("$ {IT}")});
}
}
c) Output:
__timestamp
Job
Class
Metaclass
Name
d) Description:
i. Closure is similar to anonymous internal class, she is anonymous method.
Ii. By default, Closure has a parameter called IT.
Iii. Eachxx () method. This part of the method is that Groovy is additionally increased, and for Object has EachProperty (). For the file has an Eachline () method, these methods provide an EACH () method, which provides a way to traverse the internal properties, these The method receives a Closure as a parameter, so we can see the internal conditions of the class by incoming different closures.
6. Closure declaration
a) Objective: Demonstrate the declaration of Closure.
b) Syntax: {[ClosureArguments |] Statements}
c) code:
Class gtest6 {
a;
Hello (b) {
C = "c";
Return {Print ("$ {a} $ {b} $ {c}")}
}
Static void main (args) {
T = new gtest6 (A: "a")
Cr = T.Hello ("B")
Cr.call ()
}
}
d) Output:
A b C
e) Description:
i. Closure can access all variables within the definition of all she belong.
ii. Closure can be called by a call () method, even if she has left the declaration of her scope, they can still access these variables.
7. Using Closure to achieve polymorphism
a) Objective: Demonstrate how to achieve polymorphism through Closure
b) code:
Class Animal {
Action
Hello (Sound) {
Action (Sound);
}
}
DOG = {Sound | Println ("I am A Dog $ {Sound})}
CAT = {Sound | Println ("I am a cat $ {sound})}
A1 = New Animal (Action: DOG)
A2 = New Animal (Action: Cat)
A1.Hello ("WO")
A2.hello ("miao")
c) Output:
I am a dog wo
I am a cat miao
d) Description:
i. Class files in Groovy may not require the main () method, and can be executed.
II. We can achieve polymorphism by incoming different clations. For Closure, we can call them directly without calling the CALL method, so we cannot perform closure initialization similar to Dog ("WO"), as this will directly lead to the call of Closure. So, here we wrap a bend, call the Closure by the parameters of Hello.
8. Method definition
a) Objective: Demonstrate the method defined inside the class.
b) code:
DEF foo (value) {
Println "Calling function foo () with param $ {value}
}
Foo (1)
c) Output:
Calling function foo () with param 1
9. Collection statement
a) Objective: Demonstrate how to declare, access, and modify elements in the collection. Groovy provides four collection types, listings, a mapping table (MAPS), a string, array.
b) code:
Class gtest9 {
Static void main (args) {
List = [1, 2, 3, 4]
Range = 'a' .. 'd'
Map = ["a": 1, "B": 2, "C": 3]
Str = "Hello World"
Println ("===== before start =====")
Println ("The value of list [1] is $ {list [1]})
Println ("The Value of Range [1] IS $ {Range [-1]}")
Println ("The value of map.a is $ {map.a}")
Println ("" The value of str [1] is $ {str [1]})
Println ("===== Before end =====")
List [1] = 5
Map.a = 5
Println ("===== after start =====")
Println ("The value of list [1] is $ {list [1]})
Println ("The value of map.a is $ {map.a}")
Println ("===== atter End =====")
}
}
c) Output:
===== before start =====
The value of list [1] is 2
The value of range [1] is d
The value of map.a IS 1
The value of str [1] is e ===== before end =====
===== after start =====
The value of list [1] is 5
The value of map.a IS 5
===== after End =====
d) Description:
i. We cannot modify the contents of the RANGE and strings.
Ii. The keywords in Maps must be a string type.
III. In addition to the use of a forward index access collection, we can use the reverse index (ie negative).
10. Set fragment
a) Objective: Demonstrate how to achieve a collection of sections through Range. A collection of slices includes a list, a string, an array, and a regular basin.
b) code:
Class gtest10 {
Emit (col) {
For (i in col)
Print i ""
Println ("" ")
}
Static void main (args) {
T = new gtest10 ()
List = [1, 2, 3, 4]
Sublist = list [2, 3]
T. Emit (SUBLIST)
Str = "Hello World"
Substr = STR [-1, -2, -3]
T. Emit (SUBSTR)
Array = new int [] {1, 2, 3, 4, 5}
SUBARRAY = array [3..1]
T. Emit (SUBARRAY)
}
}
c) Output:
3 4
D L r
4 3 2
d) Description:
i. In addition to using a forward slice, we can use the reverse slice (ie, the range of B ".
11. Flow control
a) Objective: Demonstrate the special FOR operations in Groovy, traverse operation, and Switch operations. Through the above example, we can see that Groovy uses the FOR cycle operation with JDK 5.0. And through the Eachxx () method provided by Groovy, we can easier traversal of some specific classes. For Switch Actions, in addition to the Switch type supported by Java, Groovy also provides additional support.
b) code:
Class gtest11 {
Static void main (args) {
a = "123"
Switch (a) {
Case String: Print ("this is string")
Break;
Case Integer: Print ("this is integer")
Break;
}
PRINTLN ("" ");
B = 1
Switch (b) {
Case 0..2: Print ("in 0..2");
Break;
Case 3..5: Print ("in 3..5");
Break;
}
PRINTLN ("" ");
Switch (b) {
Case [1, 2, 3, 4]: Print ("IN [1..4]");
Break;
Case [5, 6, 7, 8]: Print ("in [5..8]"); Break;
}
}
}
c) Output:
This is string
in 0..2
In [1..4]
d) Description:
i. Case's type in Groovy's Switch can also be Class, a collection, and regular expression.
12. File operation
a) Purpose: Demonstrate the file read and write in Groovy.
b) code:
Import java.io. *
BW = New file ("test.txt"). NewWriter ()
For (i in 0 ... 10) {
Bw.writeLine (String.Valueof (i));
}
Bw.flush ();
BW.CLOSE ();
FILE = New File ("Test.txt")
File.eachline {Print "$ {it} / t"}
c) Output:
0 1 2 3 4 5 6 7 8 9
d) Description:
i. Groovy provides additional methods for the FILE class for direct access, output streams, and Reader / Writer from the File instance.
Ii. Groovy provides additional methods for Reader / Writer for direct reading and writing files.
Iii. Groovy provides additional methods for File for traversing files.
13. Command line call
a) Purpose: Demonstrate how to create Process under Groovy and call other EXE commands.
b) code:
P = "java" .execute ()
Println ("$ {p.Text});
c) Output:
USAGE: JAVA [-Options] Class [args ...]
(To Execute a class)
or java [-Options] -jar jarfile [args ...]
(To Execute A Jar File)
WHERE OPTIONS INCLUDE:
-client to select the "client" VM
-server to select the "server" VM
...
d) Description:
14. Conclusion: The power of Groovy is not in Groovy itself, but Groovy improves our work efficiency by integrating and modifying our work. A more obvious use is the XXXBuilder defined in Groovy, which simplifies our processing and use of interfaces and tools such as DOM, SAX, ANT. The implementation of Builder has largely rely on the Closure mechanism provided by Groovy, so, in general, only to master Closure can really use Groovy.
For Groovy, the use of Groovy is used to use Groovy to perform unit testing, using Groovy to customize the customization of Ant, using Groovy to perform database queries, using Groovy Swing interface design, and so on.