The document shows an example Java class called HelloWorld that contains a greeting method to return the string "Hello" plus a name. It then demonstrates initializing an instance of the class, setting the name, and calling the greet method to print the greeting. The document also shows how this example is translated to use Groovy instead of Java.
7. public class HelloWorld {
String name;
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public String greet(){
return "Hello "+name;
}
public static void main(String[] args){
HelloWorld helloWorld = new HelloWorld();
helloWorld.setName("Groovy");
System.out.println(helloWorld.greet());
}
}
8. public class HelloWorld {
String name;
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public String greet(){
return "Hello "+name;
}
public static void main(String[] args){
HelloWorld helloWorld = new HelloWorld();
helloWorld.setName("Groovy");
System.out.println(helloWorld.greet());
}
}
9. public class HelloWorld {
String name
public void setName(String name){
this.name = name
}
public String getName(){
return name
}
public String greet(){
return "Hello "+name
}
public static void main(String[] args){
HelloWorld helloWorld = new HelloWorld()
helloWorld.setName("Groovy")
System.out.println(helloWorld.greet())
}
}
10. public class HelloWorld {
String name
public String greet(){
return "Hello "+name
}
public static void main(String[] args){
HelloWorld helloWorld = new HelloWorld()
helloWorld.setName("Groovy")
System.out.println(helloWorld.greet())
}
}
11. public class HelloWorld {
String name
public String greet(){
return "Hello "+name
}
}
HelloWorld helloWorld = new HelloWorld()
helloWorld.setName("Groovy")
System.out.println(helloWorld.greet())
12. class HelloWorld {
String name
def greet(){
return "Hello "+name
}
}
def helloWorld = new HelloWorld()
helloWorld.setName("Groovy")
println(helloWorld.greet())
13. class HelloWorld {
String name
def greet(){
return "Hello "+name
}
}
def helloWorld = new HelloWorld()
helloWorld.name = "Groovy"
println helloWorld.greet()
14. class HelloWorld {
String name
def greet(){ "Hello $name"}
}
def helloWorld = new HelloWorld()
helloWorld.name = "Groovy"
println helloWorld.greet()
15. class HelloWorld {
String name
def greet(){ "Hello $name"}
}
def helloWorld = new HelloWorld(name: "Groovy")
println helloWorld.greet()