-
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.
- 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) } }