groovy program structure

Posted by keenlearner on Sat, 07 Dec 2019 13:09:15 +0100

Definition package

The definition of a package is the same as in Java:

// defining a package named com.yoursite
package com.yoursite

Import package

1.Import package
//After the class name in the package is imported, you can directly use the class name without adding the limited name
// importing the class MarkupBuilder
import groovy.xml.MarkupBuilder

// using the imported class to create an object
def xml = new MarkupBuilder()

assert xml != null

2.Default import:The following classes are Groovy Has been imported by default, and can be used directly
import java.lang.*
import java.util.*
import java.io.*
import java.net.*
import groovy.lang.*
import groovy.util.*
import java.math.BigInteger
import java.math.BigDecimal

3.Routine introduction:Simple import is regular import
// importing the class MarkupBuilder
import groovy.xml.MarkupBuilder

// using the imported class to create an object
def xml = new MarkupBuilder()

assert xml != null

4.Asterisk import:Asterisk import can import all the classes in the package at once,The problem with asterisk import is that the imported class name may conflict with the local class name
import groovy.xml.*

def markupBuilder = new MarkupBuilder()

assert markupBuilder != null

assert new StreamingMarkupBuilder() != null

5.Static import:In general, to use static methods or static properties in a class, you need to add the class name as the qualification.
import static Boolean.FALSE

assert !FALSE //use directly, without Boolean prefix!

6.Static import alias:The static import method name is not intuitive. You can use as Give it an alias.
import static Calendar.getInstance as now

assert now().class == Calendar.getInstance().class

7.Asterisk static import:You can also use asterisks to import all static attributes and static methods in a class at once
import static java.lang.Math.*

assert sin(0) == 0.0
assert cos(0) == 1.0

8.Alias the imported class. For example: give an alias to the wrong class, define a class with the same name as the original one, and expand the original wrong class,
//And correct the wrong way.
import thirdpartylib.MultiplyTwo as OrigMultiplyTwo

class MultiplyTwo extends OrigMultiplyTwo {
    def multiply(def value) {
        return value * 2 // fixed here
    }
}

// nothing to change below here
def multiplylib = new MultiplyTwo()

// assert passes as well
assert 4 == new MultiplyTwo().multiply(2)

Script VS class

Like Java, Groovy can start programs with a main class or run a script directly, such as the Main.groovy script file:

println 'Groovy world!'

Scripts are compiled into classes

The script is always compiled into a class with the same class name and script name, and the code is placed in the run method.

import org.codehaus.groovy.runtime.InvokerHelper

class Main extends Script {                     
    def run() {                                 
        println 'Groovy world!'                 
    }
    static void main(String[] args) {           
        InvokerHelper.runScript(Main, args)     
    }
}

Methods in scripts

Methods can be defined in scripts
int fib(int n) {
    n < 2 ? 1 : fib(n-1) + fib(n-2)
}
assert fib(10)==89

//Method calls and definitions can be placed in scripts
println 'Hello'                                 
int power(int n) { 2**n }                       
println "2^6==${power(6)}" 

//The methods in the script are actually defined in the script class, and all execution code is placed in the run method. The above code will be converted to the following:
import org.codehaus.groovy.runtime.InvokerHelper
class Main extends Script {
    int power(int n) { 2** n}                   
    def run() {
        println 'Hello'                         
        println "2^6==${power(6)}"              
    }
    static void main(String[] args) {
        InvokerHelper.runScript(Main, args)
    }
}

Variables in script

1.Can be directed to Java As in, declare before using:
int x = 1
int y = 2
assert x+y == 3

2.You can also use an undeclared variable directly:
x = 1
y = 2
assert x+y == 3

Variables declared with the first method cannot be used in methods in the script, nor can they be seen outside the script.

Variables declared with the second method can be used in methods in the script and can be seen outside the script.

Note source: https://www.jianshu.com/p/1339ab4f35dd

Source: http://groovy-lang.org/syntax.html

Topics: Java xml Asterisk