2. The basic grammar of Groovy (1)

1. Variable types in
groovy Although basic variable types such as int\double can be used in groovy, they are actually object types Integer\Double. But in use, it has no effect on us.

Second, the definition of variables
1. Strong type definition: specify the type of the variable when defining the variable. For example, int x = 10
2. The weak type def definition method: The compiler will automatically determine the type of the value according to the type, so there is no need to specify the type of the variable. For example, dev x = 11
3. If the variable is only used for this class and this module, and not for other uses, you can use the def method for weak type definition, and we can redefine it to other types at any time. Therefore, if it is used by other modules, it is best to define it as a strong type to ensure that the type passed to us from the outside is correct.
Insert picture description here

Third, the definition of
string String, GString:
1) The best way is to define by single quotation marks (of course, double quotation marks can also be defined), java is a string defined by double quotation marks. There is no format for single-quoted strings, which means that if you want to concatenate line strings or newlines, you need to use the + sign and newline characters for operation.

	2)三引号字符串,支持格式
	3)双引号字符串:可扩展字符串,可以加变量的,而单引号则是不可以加变量的。但是双引号定义出来的,将不再是java.lang.String类型的字符串了,而是GStringImpl类型的字符串了。

Insert picture description here

	小结:String类型和GString类型可以互相转换传递的,是由编译器帮我们实现了。所以在平时编码时,不用刻意的去在意字符串类型是哪种?需不需要强制转换后进行参数传递?完全不需要考虑这些。

4. Newly added APIs for strings.
Commonly used APIs include all methods in the following: java.lang.String, DefaultGroovyMethods, StringGroovyMethods: common type parameters, closure type parameters.

StringGroovyMethods中的普通类型的参数:
1、字符串扩展填充方法

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

5. Logic control
1. Sequential logic: single step down execution

2、条件逻辑:if/else(在groovy中没什么变化),switch/case
	java中switch/case传入的类型限制为:int,char,string,enum四种类型,groovy中就没有限制了,可以传入任何类型的变量进行判断
	
3、循环逻辑:while循环(在groovy中没什么变化),for循环

Insert picture description here
Insert picture description here

Author: Cangshuipu witch cloud
blog: http: //blog.csdn.NET/amir_zt/
more original, please indicate the source, thank you.
https://blog.csdn.net/amir_zt/article/details/113450130

Guess you like

Origin blog.csdn.net/u011635351/article/details/113450130