Groovy learning record 01

Groovy's Api documentation

1. What is Groovy?

Two, Groovy and Java comparison

(1) Groovy is fully compatible with Java syntax, that is, it is executable to write java code in Groovy. Because both are based on JVM, Groovy is finally compiled into java bytecode
(2) The semicolon at the end of the code line is optional
(3) The classes and methods are public by default
(4) The compiler gives attributes Automatically add getter/setter methods
(5) Attributes can be obtained by dots
(6) The value of the last expression in the method will be used as the return value, that is, if a method has a return value, then the return keyword can be omitted
( 7) == is equivalent to equals(), there will be no NullPointerExceptions, but a null pointer exception will still be reported when calling properties or methods with a null object directly

Three, high-efficiency characteristics

(1) Self-contained assertion statement, namely assert statement
(2) Optional type definition, affirm that the variable can use "def variable name", the specific type of the variable is deduced according to the following operation
(3) Optional brackets, call method If there are parameters, the parentheses can be omitted
(4) string. Groovy's string has three expressions: single quote, double quote, three single quotes
(5) Collection API, List and Map in Groovy are more efficient wording
(6) support closures

Fourth, use IDEA to build a Gradle project

4.1 Steps to create a project

4.2 Open the Groovy Console controller

Five, Groovy language feature test

5.1 For the feature test of Groovy language one

public class ProjectVersion {
    private int major;
    private int minor; //没有给minor添加set/get方法

    ProjectVersion(int major, int minor) {
        this.major = major  //语句末尾没有分号,成功编译执行
        this.minor = minor
    }

    int getMajor() {
        major //最后一个表达式作为返回值
    }

    void setMajor(int major) {
        this.major = major
    }

}

ProjectVersion v1 = new ProjectVersion(2, 1);
println v1.major; //打印成功
println v1.minor; //打印成功

ProjectVersion v2 = null;
println v2==v1  //false,未报空指针异常
println v2.minor //报空指针异常

5.2 For the feature test of Groovy language 2

//Groovy的高效特性
//1 可选的类型定义
def version = 1;

//2 assert断言
//assert version == 2;

//3 括号是可选的
println version

//4 字符串
def s1 = 'groovy';

def s2 = "gradle version is ${version}"

def s3 = '''gradle
is
good tools'''

println s1
println s2
println s3

//5 集合
//list
def buildTools = ['ant', 'maven'];
buildTools << 'gradle'
assert buildTools.getClass() == ArrayList
assert buildTools.size() == 3

//map
def buildMap = ["ant": "第一代工具", "maven": '第二代工具'];
buildMap.gradle="第三代工具";

println buildMap.size();
println buildMap
println buildMap.ant
println buildMap.getClass() //LinkedHashMap

//6 闭包
def c1={
    v->println v
}

def  c2={
    println 'hello'
}

//这里的Closure不能导包,它是Groovy自带的
def method1(Closure closure){
    closure('有参数')
}

def method2(Closure closure){
    closure()
}

method1(c1)
method2(c2)

Guess you like

Origin blog.csdn.net/Duckdan/article/details/109550840