Continue with the Basics by Example; today's version of the post written in Groovy Enjoy!
You can copy and paste the code below in your favorite IDE/Editor and start playing and learning with it. This little "working" program will teach you the basics of the Programming Language.
There are some "comments" on the code added just to tell you what are or how are some features called. In case you want to review the theory, you can read my previous post, where I give a definition of each of the concepts mentioned on the code. You can find it here: http://carlosqt.blogspot.com/2010/08/new-series-languages-basics-by-example.html
Greetings Program - Verbose
// Groovy Basics package GvyGreetProgram public class Greet { // Fields or Attributes private String message private String name private Integer loopMessage // Properties or Getters and Setters public String getMessage() { return this.message } public void setMessage(String val) { this.message = this.Capitalize(val) } public String getName() { return this.name } public void setName(String val) { this.name = this.Capitalize(val) } public Integer getLoopMessage() { return this.loopMessage } public void setLoopMessage(Integer val) { this.loopMessage = val } // Constructor public def Greet() { this.message = "" this.name = "" this.loopMessage = 0 } // Overloaded Constructor public def Greet(String message, String name, Integer loopMessage) { this.message = this.Capitalize(message) this.name = this.Capitalize(name) this.loopMessage = loopMessage } // Method 1 private String Capitalize(String val) { // "if-then-else" statement if (val.size() >= 1) { return val[0].toUpperCase() + val[1..-1] } else { return ""; } } // Method 2 public void Salute() { // "for" statement for (i in 1..this.loopMessage) { println "${this.message} ${this.name}!" } } // Overloaded Method 2.1 public void Salute(String message, String name, Integer loopMessage) { // "while" statement Integer i = 0 while(i < loopMessage) { println "${this.Capitalize(message)} ${this.Capitalize(name)}!" i++ } } // Overloaded Method 2.2 public void Salute(String name) { // "switch/case" statement def dtNow = new GregorianCalendar() switch (dtNow.get(Calendar.HOUR_OF_DAY)) { case 6: case 7: case 8: case 9: case 10: case 11: this.message = 'good morning,' break; case 12: case 13: case 14: case 15: case 16: case 17: this.message = 'good afternoon,' break; case 18: case 19: case 20: case 21: case 22: this.message = 'good evening,' break; case 23: case 0: case 1: case 2: case 3: case 4: case 5: this.message = 'good night,' break; default: this.message = 'huh?' break; } println "${this.Capitalize(this.message)} ${this.Capitalize(name)}!" } } // Console Program // Define variable object of type Greet and Instantiate. Call Constructor Greet g = new Greet() // Call Setters g.setMessage('hello') g.setName('world') g.setLoopMessage(5) // Call Method 2 g.Salute() // Overloaded Method 2.1 and Getters g.Salute(g.getMessage(), 'groovy', g.getLoopMessage()) // Overloaded Method 2.2 g.Salute('carlos') // Stop and exit println "Press any key to exit..." Scanner sin = new Scanner(System.in) String line = sin.nextLine() sin.close()
Greetings Program - Minimal
// Groovy Basics class Greet { // Fields or Attributes private def message private def name private def loopMessage // Properties or Getters and Setters def getMessage() { return message } def setMessage(val) { message = Capitalize(val) } def getName() { return name } def setName(val) { name = Capitalize(val) } def getLoopMessage() { return loopMessage } def setLoopMessage(val) { loopMessage = val } // Constructor def Greet() { message = "" name = "" loopMessage = 0 } // Overloaded Constructor def Greet(message, name, loopMessage) { this.message = Capitalize(message) this.name = Capitalize(name) this.loopMessage = loopMessage } // Method 1 private def Capitalize(val) { // "if-then-else" statement if (val.size() >= 1) { return val[0].toUpperCase() + val[1..-1] } else { return ""; } } // Method 2 def Salute() { // "for" statement for (i in 1..loopMessage) { println "$message $name!" } } // Overloaded Method 2.1 def Salute(message, name, loopMessage) { // "while" statement def i = 0 while(i < loopMessage) { println "${Capitalize(message)} ${Capitalize(name)}!" i++ } } // Overloaded Method 2.2 def Salute(name) { // "switch/case" statement def dtNow = new GregorianCalendar() switch (dtNow.get(Calendar.HOUR_OF_DAY)) { case 6: case 7: case 8: case 9: case 10: case 11: message = 'good morning,' break; case 12: case 13: case 14: case 15: case 16: case 17: message = 'good afternoon,' break; case 18: case 19: case 20: case 21: case 22: message = 'good evening,' break; case 23: case 0: case 1: case 2: case 3: case 4: case 5: message = 'good night,' break; default: message = 'huh?' } println "${Capitalize(message)} ${Capitalize(name)}!" } } // Console Program // Define variable object of type Greet and Instantiate. Call Constructor def g = new Greet() // Call Setters g.setMessage('hello') g.setName('world') //g.setLoopMessage(5) g.loopMessage = 5 // Call Method 2 g.Salute() // Overloaded Method 2.1 and Getters g.Salute(g.getMessage(), 'groovy', g.getLoopMessage()) // Overloaded Method 2.2 g.Salute('carlos') // Stop and exit println "Press any key to exit..." def sin = new Scanner(System.in) def line = sin.nextLine() sin.close()
And the Output is:
Auto-Implemented Properties in Groovy
Auto-implemented properties enable you to quickly specify a property of a class without having to write code to Get and Set the property. The following code shows how to use them just like with VB.NET, C#, C++/CLI and so on.
class Greet { // Fields or Attributes // and Autoimplemented Properties (Getters and Setters) // To create auto implemented setters and getters you just define the fields with no explicit accessor String message = 'empty_message' def name = 'empty_name' Integer loopMessage = 0 // if you specify an explicit accessor then you need to define your own explicit Setters and Getters // as I did in the minimal and verbose examples (because we needed the capitalize custom code in set properties) // Example: // public String message = 'empty_message' // private def name = 'empty_name' // protected Integer loopMessage = 0 // The only one that will still create an auto-implemented Setter will be "final", but no setter since // that's how you do a read-only property. def Salute() { println "$message $name $loopMessage" } } g = new Greet() g.Salute() // Calling Auto-implemented Setters g.setMessage('hello') g.setName('world') g.setLoopMessage(5) g.Salute() // we can also access the fields from the class directly g.message = 'bye' g.name = 'carlos' g.loopMessage = 2 // Calling Auto-implemented Getters println g.getMessage() + ' ' + g.getName() + ' ' + g.getLoopMessage()
And the Output is:
No comments:
Post a Comment