The introduction
In the last section, we looked at “Variable definitions.” In this chapter, we will learn a series of programming basics, such as strings/arrays/collections
String has three common definitions
(1) Single quotes define strings
def name = 'Single quotes define string'
Copy the code
This definition is unformatted, requiring line breaks and so on and can only be concatenated with a plus sign
(2) Two single quote definitions
def nam2 = "2 single quote defined strings"// Common definition
def helloExt = "hello : ${nam2}"// Contain variables
def sum = "the sum is : ${3 + 2}"// include expressions
Copy the code
This definition is extensible, can include variables/expressions, etc., is relatively common one
(3) Three single quote definitions
def name1 = ' ''3 single quotes define the first, second, and third lines of the string {name: Davi age: boy}'' '
Copy the code
This definition is more readable. You can specify a format, such as JSON
String Experience related API
Let’s define:
def str = "string1"
def str2 = "string2"
Copy the code
String extension, central padding:
str.center(5.'xx')
Copy the code
Left to fill:
str.padLeft(5.'aa')
Copy the code
Index:
println str[0]// Access a single element
println str[0.1.]// Access elements from 0 to 1
Copy the code
closure
A closure can be a function that reads variables inside other functions.
def closure1 = {
println 'hello closure! '
}
closure1.call()
Copy the code
Definitions and calls with arguments:
def closure2 = {
String name, int age -> println "hello ${name}"
}
closure2('closure??? '.9)
Copy the code
It default parameter use:
def closure3 = {
println "hello ${it}"
}
closure3.call("It default parameter...")
Copy the code
Definitions and calls with return values:
def closure4 = {
return 'Closure return value'
}
println closure4.call()
Copy the code
The use of closures in conjunction with basic data types, etc
And int, n factorial, no need for loop, concise code:
int fab(int num) {
int result = 1
//1 loops to num
1.upto(num, { n -> result *= n })
return result
}
Copy the code
String, in combination with closures, iterates through characters
void strFab(a) {
def str = '2 and 3 is 5'
str.each {
String s -> println a
}
}
Copy the code
Closures are important for three variables
Three important variables: this, owner, and delegate
This: represents the class at the closure definition
Owner: Class/object at the closure definition
Delegate: Any object. Default is the same as ‘owner’
In most cases, the three keywords agree;
If there are nested closures in the closure, then ‘this’ points to the outermost layer and ‘owner’ and ‘delegate’ points to the nearest layer
void demo(a) {
// Three important variables
def closure = {
// Class at the closure definition, such as pluginimp.groovy
println "closure this :" + this
// The class/object at the closure definition
println "closure owner :" + owner
// Any object. Default is the same as 'owner'
println "closure delegate :" + delegate
def inner = {
println "inner this :" + this
println "inner owner :" + owner
println "inner delegate :" + delegate
}
}
closure.call()
}
Copy the code
At the end
Haha, that’s all for this article (systematic learning and growing together)