thinking in Groovy the first chapter simplifies Java code with groovy
Author: Chszs, reprint should be indicated. Blog home: Http://blog.csdn.net/chszs
1. Groovy's installation is currently in the latest version of Groovy is 2.1.2 version, download address: http://groovy.codehaus.org/Download
After downloading, unzip groovy-binary-2.1.2.zip to a directory, and then set the GROOVY_HOME environment variable to point to Groovy's decompression directory.
Then set the PATH environment variable and add%groovy_home%\bin.
To verify that the above installation is correct, on the command line, execute:
>groovy-v
Groovy version:2.1.2 jvm:1.7.0_09 vendor:oracle Corporation os:windows 7
Indicates groovy installation was successful.
2. Groovy's first exampleLet's write a Java class Todo.java, which reads as follows:
Import java.util.List;
Import java.util.ArrayList;
public class todo{
private String name;
private String note;
Public Todo () {} public
todo (string name, string note) {
this.name = name;
This.note = Note;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
Public String Getnote () {return note
;
}
public void Setnote (String note) {
this.note = Note;
}
public static void Main (string[] args) {
list<todo> todos = new arraylist<todo> ();
Todos.add (New Todo ("1", "one"));
Todos.add (New Todo ("2", "two"));
Todos.add (New Todo ("3", "three"));
for (Todo todo:todos) {
System.out.println (todo.getname () + "" + todo.getnote ());}}}
We compile it with Javac, run it in Java, and everything works. Now, we change the name of Todo.java to Todo.groovy, and then execute:
> Groovy Todo.groovy
We'll be amazed to see the same results as Java's execution.
Conclusion: Groovy is compatible with Java.
Running Java takes two steps: Compiling the class file with Javac, and then executing the compiled bytecode file on the JVM using Java.
Groovy, which compiles bytecode files at run time, saves the step of compiling in development.
3. Simplify Java code with groovyBelow, we use groovy to simplify JavaBean for Groovybean.
Import java.util.List;
Import java.util.ArrayList;
public class todo{
String name;
String Note;
public static void Main (string[] args) {
list<todo> todos = new arraylist<todo> ();
Todos.add (New Todo (Name: "1", note: "one"));
Todos.add (New Todo (Name: "2", note: "two"));
Todos.add (New Todo (Name: "3", note: "three"));
for (Todo todo:todos) {
System.out.println (todo.name + "" + Todo.note);}}}
The execution results are still the same.
Visible
1 omitted the getter and setter method of the JavaBean;
2) By default, all class attributes are public;
3 The constructor function is very characteristic, the map way.
4. Use Groovy to further simplify the code
public class todo{
string name
string Note Public
static void Main (string[] args) {
def todos = new Arrayli St ()
Todos.add (New Todo (name: "1", note: "One")
Todos.add (New Todo (Name: "2", note: "two"))
Todos.add (new TODO (name: "3", note: "three")) for
(Todo todo:todos)
println "${todo.name} ${todo.note}"
}
The execution results are still the same.
Visible
1) List and ArrayList omitted the type;
2) omitting the semicolon;
3 The PRINT statement omits System.out.
5. Further omit the main () function with groovy
public class todo{
string name
string Note
}
def todos = [
new Todo (name: ' 1 ', note: ' One '),
new TODO (name: "2", note: "Two"),
new Todo (Name: "3", note: "three")
]
todos.each{
println "${it.name} ${ It.note} "
}
This code works fine on the Groovyconsole console.
Looking at the above, what do you think of groovy? In the next section, we will describe the key features of the groovy language.