Groovy 基本チュートリアル

I. 概要

Groovy は、JVM (Java Virtual Machine) に基づいたアジャイル開発言語です。Python、Ruby、Smalltalk の機能を組み合わせています。Groovy コードは Java コードとうまく組み合わせることができ、既存のコードの拡張にも使用できます。Groovy は JVM 上で実行されるという性質上、他の Java 言語で書かれたライブラリを使用できます。

Groovy と Java の違いを示す簡単な出力例:

//java写法
class FuncITFeiniu{
    
     
    public static void mian(String[] args){
    
     
        println("Hello World!");
    }
}
//groovy写法 文件名:chapter01.groovy
println("Hello World!");

//执行
groovy chapter01.groovy
groovy chapter01

2. インストールウィンドウ10

1. ダウンロード

groovy操作は依存しているJDKため、最初にインストールしJDKてからインストールする必要があることに注意してくださいgroovy

JDKのダウンロードアドレス: https://www.oracle.com/java/technologies/downloads/

Groovy のダウンロード アドレス: http://www.groovy-lang.org/download.html

apache-groovy-sdk-4.0.12.zipこのような 2 つの zip パッケージがダウンロードされますjdk-20_windows-x64_bin.zip以下に示すように:

ここに画像の説明を挿入

ここでは*.zipインストール パッケージを選択しますが、インストールを選択msiまたは自動化することもできます。exe

2. 環境変数を設定する

上記 2 つのパッケージを解凍し、[環境変数] を開きます。
ここに画像の説明を挿入

  1. JAVA_HOMEシステム変数を構成する
    ここに画像の説明を挿入

  2. PATH、JDK コマンド ラインを追加します。%JAVA_HOME%\bin

  3. PATH、groovy コマンド ラインを追加します。C:\Program Files\groovy-4.0.12\bin
    ここに画像の説明を挿入

この時点で、インストールは完了です。cmdコマンド ライン ツールを開いて、インストールが成功したかどうかを確認してみましょう。 と をjava -versionそれぞれ入力groovy -versionして、バージョン番号が正常に出力されるかどうかを確認します。正常に出力されれば、インストールは成功しています。
ここに画像の説明を挿入

3. デフォルトでインポートする

デフォルトでは、Groovy にはコードに次のライブラリが含まれているため、それらを明示的にインポートする必要はありません。

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

4. データ型

1. 組み込みのデータ型

Groovy にはいくつかの組み込みデータ型が用意されています。以下は、Groovy で定義されているデータ型のリストです。

  • byte - バイト値を表すために使用されます。たとえば2。
  • short - これは短い整数を表すために使用されます。たとえば10。
  • int - 整数を表すために使用されます。たとえば、1234。
  • long - これは長整数を表すために使用されます。たとえば、10000090。
  • float - 32 ビット浮動小数点数を表すために使用されます。たとえば、12.34。
  • double - これは、64 ビット浮動小数点数を表すために使用されます。これは、場合によっては必要になるより長い 10 進表現です。たとえば、12.3456565。
  • char - 単一文字リテラルを定義します。たとえば「A」。
  • ブール値- これは、true または false のブール値を表します。
  • 文字列- これらは文字列として表されるテキストです。たとえば、「ハローワールド」。

数値型の値の許容範囲:

バイト -128~127 -2^7 ~ +2^7
短い -32,768 ~ 32,767 -2^15 ~ +2^15
整数 -2,147,483,648 ~ 2,147,483,647 -2^31 ~ +2^31
長さ -9,223,372,036,854,775,808 ~ +9,223,372,036,854,775,807 -2^63 ~ +2^63
浮く 1.40129846432481707e-45 ~ 3.40282346638528860e+38 -2^127 ~ +2^127
ダブル 4.94065645841246544e-324d から 1.79769313486231570e+308d -2^255 ~ +2^255
class Example {
    
     
   static void main(String[] args) {
    
     
      //整型 
      int x = 5; 
      //长整型 
      long y = 100L; 
		
      //单精度型 
      float a = 10.56f; 
		
      //双精度型 
      double b = 10.5e40; 
		
      //不可变的任意精度的有符号整数数字
      BigInteger bi = 30g; 
		
      //不可变的任意精度的有符号十进制数
      BigDecimal bd = 3.5g; 
		
      println(x); 
      println(y); 
      println(a); 
      println(b); 
      println(bi); 
      println(bd); 
   } 
}
//打印内容:
//5 
//100 
//10.56 
//1.05E41 
//30 
//3.5

2. 動的変数

組み込み変数タイプを使用して変数が定義されている場合、一度定義すると、後で変数タイプを変更することはできません。ただし、のような任意の変数型を定義するためのキーワードGroovyもありますjavascriptanydef

class Example {
    
     
   static void main(String[] args) {
    
     
      def _Name = 1;
      _Name="it飞牛"; 
      println(_Name);
   } 
}
//打印如下:
it飞牛

5. オペレーター

1. 算術演算子

オペレーター 説明
+ 2 つのオペランドの加算 1 + 2 は 3 になります
- 第 1 オペランドと第 2 オペランドの減算 2 - 1 は 1 を獲得します
* 2 つのオペランドの乗算 2 * 2 は 4 になります
/ 2 つのオペランドの除算 3/2 は 1.5 になります
モジュロ演算 2 の 3% が 1 を獲得します
++ 自身の値に 1 を加算する自己インクリメント演算 INT X = 5;X ++;X は 6 を取得します
自己減算演算、自身の値から 1 を減算します。 INT X = 5;X - -;X は 4 を取得します

2. 関係演算子

オペレーター 説明
== 2 つのオブジェクト間の同等性をテストします 2 == 2 が true になります
!= 2 つのオブジェクト間の不等性をテストします 3 != 2 が true になります
< 左側のオブジェクトが右側のオブジェクトより小さいかどうかを確認します。 2 < 3 が true になります
<= 左側のオブジェクトが右側のオブジェクト以下であるかどうかを確認します 2 <= 3 が true になります
> 左側のオブジェクトが右側のオブジェクトより大きいかどうかを確認します。 3 > 2 が true になります
>= 左側のオブジェクトが右側のオブジェクト以上であるかどうかを確認します。 3>= 2 が true になります

3. 論理演算子

オペレーター 説明
&& これは論理積演算です true && true get true
|| これは論理和演算です true || true は true になります
これは論理否定演算です !true は false になります

4. ビット演算子

オペレーター 説明
これはビットごとの AND 演算です
| これはビットごとの OR 演算です
^ これはビット単位の「排他的論理和」または排他的論理和演算子です。
これはビットごとの逆演算子です

5. 代入演算子

オペレーター 説明
+= A += B 等了 A = A+B DEF A = 5A += 3 出力は 8 になります
-= A -= B 等了 A = AB DEF A = 5A -= 3 出力は 2 になります
*= A = B は A = A Bと同等です DEF A=5A ※=3の出力は15となります
/= A /= B 等价于 A = A/B DEF A = 6A /= 3输出将是2
(%)= A (%)= B 等价于 A = A % B DEF A = 5A %= 3输出将是2

6、范围运算符

Groovy支持范围的概念,并在..符号的帮助下提供范围运算符的符号。

class Example {
    
     
   static void main(String[] args) {
    
     
      def range = 5..10; 
      println(range); 
      println(range.get(2)); 
   } 
}
//打印如下:
5..10
6
7
8
9
10

六、循环

1、while语句

class Example {
    
    
   static void main(String[] args) {
    
    
      int count = 0;
        
      while(count<5) {
    
    
         println(count);
         count++;
      }
   }
}

2、for语句

class Example {
    
     
   static void main(String[] args) {
    
    
    
      for(int i = 0;i<5;i++) {
    
    
         println(i);
      }
        
   }
}

3、for-in语句

class Example {
    
     
   static void main(String[] args) {
    
     
      int[] array = [0,1,2,3]; 
        
      for(int i in array) {
    
     
         println(i); 
      } 
   } 
}

4、循环控制语句

序号 语句和描述
1 break语句break语句用于改变循环和switch语句内的控制流。
2 continue语句continue语句补充了break语句。它的使用仅限于while和for循环。

七、条件语句

1、if语句

class Example {
    
     
   static void main(String[] args) {
    
     
      // Initializing a local variable 
      int a = 2 
        
      //Check for the boolean condition 
      if (a<100) {
    
     
         //If the condition is true print the following statement 
         println("The value is less than 100"); 
      } 
   } 
}

2、if / else语句

class Example {
    
     
   static void main(String[] args) {
    
     
      // Initializing a local variable 
      int a = 2
        
      //Check for the boolean condition 
      if (a<100) {
    
     
         //If the condition is true print the following statement 
         println("The value is less than 100"); 
      } else {
    
     
         //If the condition is false print the following statement 
         println("The value is greater than 100"); 
      } 
   } 
}

3、Switch语句

class Example {
    
     
   static void main(String[] args) {
    
     
      //initializing a local variable 
      int a = 2
        
      //Evaluating the expression value 
      switch(a) {
    
                
         //There is case statement defined for 4 cases 
         // Each case statement section has a break condition to exit the loop 
            
         case 1: 
            println("The value of a is One"); 
            break; 
         case 2: 
            println("The value of a is Two"); 
            break; 
         case 3: 
            println("The value of a is Three"); 
            break; 
         case 4: 
            println("The value of a is Four"); 
            break; 
         default: 
            println("The value is unknown"); 
            break; 
      }
   }
}

八、方法

class Example {
    
    
   static def DisplayName() {
    
    
      println("This is how methods work in groovy");
      println("This is an example of a simple method");
   } 
	
   static void main(String[] args) {
    
    
      DisplayName();
   } 
}

参数和返回值

  • 支持传入多个,以,号分割,并配以类型关键字。
  • 支持设置默认值
  • 返回值可以是基础类型、def、void

案例1:

class Example {
    
    
   static int sum(int a,int b = 5) {
    
    
      int c = a+b;
      return c;
   } 
	
   static void main(String[] args) {
    
    
      println(sum(6));
   } 
}
//打印:
//11

案例2:

class Example {
    
     
   static int x = 100; 
	
   public static int getX() {
    
     
      int lx = 200; 
      println(lx); 
      return x; 
   } 
	
   static void main(String[] args) {
    
     
      println getX() 
   }  
}
//打印:
//200
//100

案例3:

class Example {
    
     
   int x = 100; 
	
   public int getX() {
    
     
      this.x = 200; 
      return x; 
   } 
	
   static void main(String[] args) {
    
    
      Example ex = new Example(); 
      println(ex.getX());
   }
}
//打印:
//200

九、文件I/O

Groovy在使用I / O时提供了许多辅助方法,Groovy提供了更简单的类来为文件提供以下功能。

  • 读取文件
  • 写入文件
  • 遍历文件树
  • 读取和写入数据对象到文件

除此之外,您始终可以使用下面列出的用于文件I / O操作的标准Java类。

  • java.io.File
  • java.io.InputStream
  • java.io.OutputStream
  • java.io.Reader
  • java.io.Writer

读取文件

import java.io.File 
class Example {
    
     
   static void main(String[] args) {
    
     
      new File("./Example.txt").eachLine {
    
      
         line -> println "line : $line"; 
      } 
   } 
}
//同目录下存在Example.txt文件,内容如下:
line : Example1
line : Example2

//执行上述代码后,控制台打印如下:
line : line : Example1
line : line : Example2

读取文件的内容到字符串

class Example {
    
     
   static void main(String[] args) {
    
     
      File file = new File("E:/Example.txt") 
      println file.text 
   } 
}
//打印:
line : Example1
line : Example2

写入文件

import java.io.File 
class Example {
    
     
   static void main(String[] args) {
    
     
      new File('./Example.txt').withWriter('utf-8') {
    
     
         writer -> writer.writeLine 'Hello World' 
      }  
   } 
}
//执行后,Example.txt中的内容会被替换为:Hello World

获取文件的大小

class Example {
    
    
   static void main(String[] args) {
    
    
      File file = new File("E:/Example.txt")
      println "The file ${file.absolutePath} has ${file.length()} bytes"
   } 
}
//打印:
//The file D:\学习\groovy\Code\groovy-project\.\Example.txt has 13 bytes

测试文件是否是目录

class Example {
    
     
   static void main(String[] args) {
    
     
      def file = new File('./') 
      println "File? ${file.isFile()}" 
      println "Directory? ${file.isDirectory()}" 
   } 
}
//打印:
//File? false
//Directory? true

创建目录

class Example {
    
    
   static void main(String[] args) {
    
    
      def file = new File('./Directory')
      file.mkdir()
   } 
}
//执行完成后,会在当前目录下新建一个 Directory 目录

删除文件

class Example {
    
    
   static void main(String[] args) {
    
    
      def file = new File('./Example.txt')
      file.delete()
   } 
}
//执行完成后,会把当前目录下的./Example.txt文件删除

复制文件

class Example {
    
    
   static void main(String[] args) {
    
    
      def src = new File("./Example.txt")
      def dst = new File("./Example1.txt")
      dst << src.text
   } 
}
//将创建文件Example1.txt,并将文件Example.txt的所有内容复制到此文件。

获取目录内容

class Example {
    
    

   static void main(String[] args) {
    
    
      def rootFiles = new File('test').listRoots()
      rootFiles.each {
    
    
         file -> println file.absolutePath
      }
   }

}
//打印:
C:\
D:\
G:\

new File()入参也可以是某个指定的目录,那么会把目录下的文件和文件夹都打印出来。例如:

D:\学习\groovy\Code\groovy-project\.\Directory
D:\学习\groovy\Code\groovy-project\.\Example.groovy
D:\学习\groovy\Code\groovy-project\.\Example.txt
D:\学习\groovy\Code\groovy-project\.\Example1.txt

如果要递归显示目录及其子目录中的所有文件,则可以使用File类的eachFileRecurse函数。以下示例显示如何完成此操作。

十、字符串

字符串可以用单引号''、""、"''"括起来。此外,由三重引号括起来的Groovy字符串可以跨越多行。

如下:

class Example {
    
    

   static void main(String[] args) {
    
    
      String a = 'Hello Single'
      String b = 'Hello Double'
      String c = """Hello Triple
      Multiple lines"""

      println(a)
      println(b)
      println(c)
   }

}

//打印:
Hello Single
Hello Double
Hello Triple
      Multiple lines

字符串索引

class Example {
    
     
   static void main(String[] args) {
    
     
      String sample = "Hello world"; 
      println(sample[4]); // Print the 5 character in the string
		
      //Print the 1st character in the string starting from the back 
      println(sample[-1]); 
      println(sample[1..2]);//Prints a string starting from Index 1 to 2 
      println(sample[4..2]);//Prints a string starting from Index 4 back to 2 
      
   } 
}
//打印:
o 
d 
el 
oll 

十一、范围

范围是指定值序列的速记。一些范例文字的例子 -

  • 1…10 - 包含范围的示例
  • 1 … <10 - 独占范围的示例
  • ‘a’…‘x’ - 范围也可以由字符组成
  • 10…1 - 范围也可以按降序排列
  • ‘x’…‘a’ - 范围也可以由字符组成并按降序排列。
def list1 = 1..10
def list2 = 1..<10
def list3 = 'a'..'x'
def list4 = 10..1
def list5 = 'x'..'a'

println list1.size()
println list2.size()
println list3.size()
println list4.size()
println list5.size()
//打印:
10
9
24
10
24

十二、列表

groovy 列表使用索引操作符 [] 索引。列表索引从 0 开始,指第一个元素。

groovy 中的一个列表中的数据可以是任意类型。这 java 下集合列表有些不同,java 下的列表是同种类型的数据集合。

List.reverse() 可以实现列表反转。调用 List.sort() 可以实现列表排序。

def list1 = []  
def list2 = [1,2,3,4]  
list2.add(12)  
list2.add(12)  
println list1.size()
println list2.size()
//打印:
0
6

1、搜索-find

class Example {
    
    

   static void main(String[] args) {
    
    
      def list = [1, 2, 3, 1, 2, 3];

      //find:返回第一个符合条件的项
      def find = list.find({
    
    
         item ->
            {
    
    
                  item == 1
            }
      })
      println(find); // 1

      //findAll:返回所有符合条件的项
      def findA = list.findAll({
    
    
         item ->
            {
    
    
                  item == 1
            }
      })
      println(findA); //[1,1]

      //findIndexOf:返回满足条件的第一个元素的下标值。
      def index = list.findIndexOf({
    
    
         item ->
            {
    
    
                  item == 1
            }
      })
      println(index); //   0

   }

}

2、排序-sort、reverse

class Example {
    
    

   static void main(String[] args) {
    
    
      def list = [1, 2, 3, 1, 2, 3];
      
      //sort-正序排序
      def list1=list.sort();
      println(list1);
      
      //sort-倒叙排序
      def list2=list.sort({
    
    
         a, b -> a - b ? -1 : 1
      });
      println(list2);

      // reverse:将原list倒序,返回一个新的list
      def list3=list.reverse();
      println(list3);
   }

}
//打印:
[1, 1, 2, 2, 3, 3]
[3, 3, 2, 2, 1, 1]
[1, 1, 2, 2, 3, 3]

3、返回新数组-collect、tail

class Example {
    
    

   static void main(String[] args) {
    
    
      def list = [1, 2, 3, 1, 2, 3];
      
      //collect:返回一个新的list,他可以接受一个闭包参数或者无参。类似js中map。it是闭包中自带的隐式变量;
      def doCollect = list.collect({
    
    
         return it - 1;
      })
      println(doCollect)

      // tail:返回一个新的list,它包含list中的所有的元素(除了第一个元素)
      def doTail=list.tail();
      println(doTail)

      // 一个累积的过程方法(有点像js的reduce),传入inject方法中的ini作为sum的初始值,在遍历collection的过程中,将处理结果(“$sum $elem”)保存到sum中。
      def doInject=list.inject("ini") {
    
    
         sum, elem -> "$elem $sum"
      }
      println(doInject)
   }

}

// 打印:
// [0, 1, 2, 0, 1, 2]
// [2, 3, 1, 2, 3]
// 3 2 1 3 2 1 ini

4、遍历

each、eachWithIndex

class Example {
    
    

   static void main(String[] args) {
    
    
      def list = [1, 2, 3, 1, 2, 3];
      
      //each:普通的迭代遍历
      list.each({
    
    
         it -> println(it)
      })

      //eachWithIndex:他的作用和each一样,但是他要传入一个闭包,有两个参数,一个是值,一个是索引。
      list.eachWithIndex {
    
     int entry, int i ->
         {
    
    
            println( "${entry}--${i}")
         }
      }
   }

}

// 打印:
// 1
// 2
// 3
// 1
// 2
// 3
// 1--0
// 2--1
// 3--2
// 1--3
// 2--4
// 3--5

every、any、first、last、max、min、count、unique

class Example {
    
    

   static void main(String[] args) {
    
    
      def list = [1, 2, 3, 1, 2, 3];
      
      //every:接收一个闭包,返回为一个布尔值,当所有条件都满足时才返回true
      def flag1=list.every({
    
    
         it -> it>0
      })
      println(flag1);

      //any:和every用法一样,只要有一个为true,则返回true
      def flag2=list.any({
    
    
         it -> it>0
      })
      println(flag2);

      //返回第一个数据
      println(list.first());
      //返回最后一个数据
      println(list.last());
      //返回最大值
      println(list.max());
      //返回最小值
      println(list.min());
      //返回总和
      println(list.sum());
      //返回某项出现的次数
      println(list.count(3));
      //返回去重后的数组
      println(list.unique());
   }

}

// 打印:
// true
// true
// 1
// 3
// 3
// 1
// 12
// 2
// [1, 2, 3]

5、分组

class Example {
    
    

   static void main(String[] args) {
    
          
      def items = [[name:"tony", age:4], [name:"tony", age: 5], [name:"alan", age:16]]
      def groups = items.groupBy {
    
    it.name}
      println(groups);
   }

}

// 打印:
// [tony:[[name:tony, age:4], [name:tony, age:5]], alan:[[name:alan, age:16]]]

6、包含

class Example {
    
    

   static void main(String[] args) {
    
    
      def list = [1, 2, 3, 1, 2, 3];
      def list2 = [1, 2, 3,4];
      
      println(list.containsAll(list2));
      println(list2.containsAll(list));
   }

}

// 打印:
// false
// true

十三、映射

映射(也称为关联数组,字典,表和散列)是对象引用的无序集合。Map集合中的元素由键值访问。 Map中使用的键可以是任何类。当我们插入到Map集合中时,需要两个值:键和值

以下是一些映射的例子

  • [‘TopicName’:‘Lists’,‘TopicName’:‘Maps’] - 具有TopicName作为键的键值对的集合及其相应的值。
  • [:] - 空映射。
class Example {
    
     
   static void main(String[] args) {
    
     
      def mp = ["TopicName" : "Maps", "TopicDescription" : "Methods in Maps"] 
      println(mp.get("TopicName")); 
      println(mp.get("Topic")); 
   } 
}

十四、日期和时间

class Example {
    
    

   static void main(String[] args) {
    
    
      Date date = new Date()

      println(date.toString())   //Mon Jun 19 13:15:20 CST 2023
      println(date.getDateString()) //2023/6/19
      println(date.dateTimeString)  //2023/6/19 13:15:20
      // 格式化时间
      println(date.format('yyyy-MM-dd HH:mm:ss'))  //2023-06-19 13:15:20

      // after 测试此日期是否在指定日期之后
      Date oldDate = new Date('05/11/2015')
      Date newDate = new Date('05/12/2022')
      Date latestDate = new Date()

      println(oldDate.after(newDate))  //false
      println(latestDate.after(newDate))  //true

      // equals 比较两个日期的相等性。当且仅当参数不为null时,结果为true,并且是表示与该对象时间相同的时间点(毫秒)的Date对象
      println(oldDate.equals(newDate)) //false
      println(latestDate.equals(newDate)) //false

      // 返回自此Date对象表示的1970年1月1日,00:00:00 GMT以来的毫秒数
      println(oldDate.getTime()) //1431273600000
   }

}
// Date.getTime() 获取时间戳
// Date.setTime(long time) 返回时间戳

十五、正则表达式

Groovy は~”pattern”正規表現サポートを使用し、指定されたパターン文字列を使用してコンパイルされた Java Pattern オブジェクトを作成します。Groovy は、=~(Matcher の作成) 演算子と==~(指定された文字列がパターンに一致するかどうかのブール値を返す) 演算子もサポートしています。

正規表現を定義する次のように~、+ stringを使用して正規表現を定義できます。/正则表达式/

class Example {
    
    

   static void main(String[] args) {
    
    
      def reg1 = ~'he*llo'
      def reg2 = /he*llo/
      println "reg1 type is ${reg1.class}"
      println "reg2 type is ${reg2.class}"
      println 'hello'.matches(reg1)
      println 'hello'.matches(reg2)
   }

}
//打印
reg1 type is class java.util.regex.Pattern
reg2 type is class java.lang.String
true
true

注:上記の例では、~と の=間にスペースが入っています=~。Groovy には演算子記号があるため、この演算子はクエリ演算子であり、文字列の後に使用され、正規表現が必要で、オブジェクトを返します。演算子もありますjava.util.regex.Matcher==~この演算子は、正規表現が後に続く一致演算子であり、返される型はBooleantype です。

def val1 = "hello" =~ "he*llo"
println val1.class
println val1.matches()

def val1 = "hello" ==~ "he*llo"
println val1.class
println val1

//打印:
class java.util.regex.Matcher
true
class java.lang.Boolean
true

一致文字列

class Example {
    
    

   static void main(String[] args) {
    
    
      def reg = ~/h(el)(lo)/
      def str = 'hello world hello nihao'
      def matcher = str =~ reg
      println 'first matched substring'
      println matcher[0]
      println matcher[0][0]
      println matcher[0][1]
      println matcher[0][2]
      println 'second matched substring'
      println matcher[0]
      println matcher[0][0]
      println matcher[0][1]
      println matcher[0][2]
   }

}
//打印
first matched substring
[hello, el, lo]
hello
el
lo
second matched substring
[hello, el, lo]
hello
el
lo

16. 例外処理

class Example {
    
    
   static void main(String[] args) {
    
    
      try {
    
    
         def arr = new int[3];
         arr[5] = 5;
      }catch(ArrayIndexOutOfBoundsException ex) {
    
    
         println("Catching the Array out of Bounds exception");
      }catch(Exception ex) {
    
    
         println("Catching the exception");
      }finally {
    
    
         println("The final block");
      }
        
      println("Let's move on after the exception");
   } 
}

//打印
Catching the Array out of Bounds exception
The final block
Let's move on after the exception

17、オブジェクト指向

1.クラス

class Example {
    
     
   static void main(String[] args) {
    
     
      Student st = new Student(); 
      st.StudentID = 1;

      st.Marks1 = 10; 
      st.name="Joe"; 
        
      println(st.name); 
      println(st.DisplayMarks()); 
   } 
} 

abstract class Person {
    
     
   public String name; 
   public Person() {
    
     } 
   abstract void DisplayMarks();
}
 
class Student extends Person {
    
     
   int StudentID 
   int Marks1; 
    
   public Student() {
    
     
      super(); 
   } 
    
   void DisplayMarks() {
    
     
      println(Marks1); 
   }  
} 
//打印
Joe 
10 

2. インターフェース

class Example {
    
    
   static void main(String[] args) {
    
    
      Student st = new Student();
      st.StudentID = 1;
      st.Marks1 = 10;
      println(st.DisplayMarks());
   } 
} 

interface Marks {
    
     
   void DisplayMarks(); 
} 

class Student implements Marks {
    
    
   int StudentID
   int Marks1;
	
   void DisplayMarks() {
    
    
      println(Marks1);
   }
}
//打印
10

18. ジェネリック医薬品

1.クラス

class Example {
    
    
   static void main(String[] args) {
    
    
      // Creating a generic List collection 
      ListType<String> lststr = new ListType<>();
      lststr.set("First String");
      println(lststr.get()); 
		
      ListType<Integer> lstint = new ListType<>();
      lstint.set(1);
      println(lstint.get());
   }
} 

public class ListType<T> {
    
    
   private T localt;
	
   public T get() {
    
    
      return this.localt;
   }
	
   public void set(T plocal) {
    
    
      this.localt = plocal;
   } 
}
//打印
First String
1

2. コレクションの一般的な使用

List などのコレクション クラスは、その型のコレクションのみがアプリケーションで受け入れられるように一般化できます。

class Example {
    
    
   static void main(String[] args) {
    
    
      // Creating a generic List collection
      List<String> list = new ArrayList<String>();
      list.add("First String");
      list.add("Second String");
      list.add("Third String");
		
      for(String str : list) {
    
    
         println(str);
      }
   } 
}

19. 特徴

特性は、trait キーワードを使用して定義されます。その後、implement キーワードを使用して、インターフェイスのような方法で特性を実装できます。

class Example {
    
    
   static void main(String[] args) {
    
    
      Student st = new Student();
      st.StudentID = 1;
      st.Marks1 = 10; 
      println(st.DisplayMarks());
   } 
} 

trait Marks {
    
     
   void DisplayMarks() {
    
    
      println("Display Marks");
   } 
} 

class Student implements Marks {
    
     
   int StudentID
   int Marks1;
}

インターフェースを実装する

class Example {
    
    
   static void main(String[] args) {
    
    
      Student st = new Student();
      st.StudentID = 1;
      st.Marks1 = 10;
		
      println(st.DisplayMarks());
      println(st.DisplayTotal());
   } 
} 

interface Total {
    
    
   void DisplayTotal() 
} 

trait Marks implements Total {
    
    
   void DisplayMarks() {
    
    
      println("Display Marks");
   }
	
   void DisplayTotal() {
    
    
      println("Display Total"); 
   } 
} 

class Student implements Marks {
    
     
   int StudentID
   int Marks1;  
} 

//打印
Display Marks 
Display Total

二十、閉会

  • クロージャは入力パラメータを受け取ることができます
  • クロージャは外部変数を参照できます
class Example {
    
         
   static void main(String[] args) {
    
    
      def str1 = "Hello";
      def clos = {
    
    param -> println "${str1} ${param}"}
      clos.call("World");
		
      // We are now changing the value of the String str1 which is referenced in the closure
      str1 = "Welcome";
      clos.call("World");
   } 
}

//打印
Hello World 
Welcome World

itクロージャで提供される暗黙的な変数です。クロージャに明示的に宣言されたパラメータがない場合に使用できます。クロージャが などremoveIfのコレクション メソッドで使用される場合、it現在の反復項目を指します。

List<Integer> integers = [1, 2, 3]
for(Integer it: integers) {
    
    print(it)}

推奨読書

グルーヴィーなチュートリアル

おすすめ

転載: blog.csdn.net/bobo789456123/article/details/131262716