-
Notifications
You must be signed in to change notification settings - Fork 0
Classes
- Declared just like Java
- Omitting the access modifier gives the class public visibility.
- In Groovy, getters and setters are automatically generated for class members.
- Can be overloaded.
- Formal parameters do not need types.
- Can be called either with positional or named parameters1
if you do not create a constructor (i.e., you use the default constructor), you always can invoke the default constructor with named parameters, which map instance properties to actual parameters:
class PersonWOConstructor {
String name
Integer age
}
// The default constructor for the class above can be invoked in any of the following ways:
def person4 = new PersonWOConstructor()
def person5 = new PersonWOConstructor(name: 'Marie')
def person6 = new PersonWOConstructor(age: 1)
def person7 = new PersonWOConstructor(name: 'Marie', age: 2)This saves you from having to overload the constructor many times to support the different parameter lists.
Declared constructors use positional parameters. However, Lists can be coerced into creating instances, when declared constructors exist2:
class PersonConstructor {
String name
Integer age
PersonConstructor(name, age) {
this.name = name
this.age = age
}
}
def person1 = new PersonConstructor('Marie', 1)
def person2 = ['Marie', 2] as PersonConstructor
PersonConstructor person3 = ['Marie', 3] Groovy supports both scripts and classes.
A class like this:
// Main.groovy
Main.groovy
class Main {
static void main(String... args) {
println 'Groovy world!'
}
}...can be written instead like this:
println 'Groovy world!'- A script is always compiled into a class. The Groovy compiler will compile the class for you, with the body of the script copied into a run method.3
- If the script is in a file, then the base name of the file is used to determine the name of the generated script class. For example, if the name of the file is Main.groovy, then the script class is going to be Main.
- Variables in a script do not require a type defination.
// This is legal, and equivalent to using "int" to declare the variables. x = 1 y = 2 assert x+y == 3
- Scripts can have methods/functions.
- Methods and functions can be mixed with immediate code.
See http://groovy-lang.org/objectorientation.html
See http://groovy-lang.org/objectorientation.html
Objects have a with() method like you see in other languages. While in JavaScript its use is discouraged for security/safety reasons, it is very much encouraged in Groovy.2 The with() method provides a concise syntax for performing multiple operations on the same object--that is, for invoke accessing methods and properties on the same object.
Example without with():
Below we are accessing properties and methods of the same object and having to repeat the object reference (e.g., server) each time.
server.name = application.name
server.status = status
server.sessionCount = 3
server.start()
server.stop()Same example with with():
The with() block simply eliminates the need to repeat the object reference in each line.
server.with {
name = application.name
status = status
sessionCount = 3
start()
stop()
}- http://groovy-lang.org/objectorientation.html
- http://groovy-lang.org/style-guide.html#_initializing_beans_with_named_parameters_and_the_default_constructor
- http://javarevisited.blogspot.com/2016/09/10-basic-differences-between-java-and-groovy-programming.html#ixzz4qyfYiBvZ
- http://groovy-lang.org/structure.html#_scripts_versus_classes
-
http://groovy-lang.org/objectorientation.html#_constructors
In Groovy there are two ways to invoke constructors: with positional parameters or named parameters. The former one is like we invoke Java constructors, while the second way allows one to specify the parameter names when invoking the constructor.
-
http://groovy-lang.org/objectorientation.html#_constructors
There is [sic] three forms of using a declared constructor. The first one is the normal Java way, with the new keyword. The others rely on coercion of lists into the desired types. In this case, it is possible to coerce with the as keyword and by statically typing the variable.
-
The script
println 'Groovy world!'is equivalent to this:import org.codehaus.groovy.runtime.InvokerHelper class Main extends Script { def run() { println 'Groovy world!' } static void main(String[] args) { InvokerHelper.runScript(Main, args) } }
-
http://groovy-lang.org/style-guide.html#_using_with_for_repeated_operations_on_the_same_bean