当前位置:网站首页>[groovy] closure (Introduction to closure class closure | closure parametertypes and maximumnumberofparameters member usage)
[groovy] closure (Introduction to closure class closure | closure parametertypes and maximumnumberofparameters member usage)
2022-07-05 04:40:00 【Programmer community】
List of articles
- One 、 Closure class Closure brief introduction
- Two 、 Closure parameterTypes and maximumNumberOfParameters Member usage
- 3、 ... and 、 Complete code example
One 、 Closure class Closure brief introduction
Closure is Closure object , Closure There are many members in the class , And encapsulated functions , Here is Closure Class inheritance structure ;
/** * stay Groovy Represents any closure object . * <p> * Groovy Allow closure instances to be called in a short form . for example : * <pre> * def a = 1 * def c = { a } * assert c() == 1 * </pre> * In order to be able to use closures in your own subclasses in this way , * You need to provide a with any signature you want doCall Method . * This ensures that the {@link#getMaximumNumberOfParameters()} * and {@link#getParameterTypes()} stay * It works without any extra code . * If not provided doCall Method , You must use closures in long form , Such as * <pre> * def a = 1 * def c = {a} * assert c.call() == 1 * </pre> * * @author <a href="mailto:[email protected]">James Strachan</a> * @author <a href="mailto:[email protected]">John Wilson</a> * @author <a href="mailto:[email protected]">Jochen Theodorou</a> * @author Graeme Rocher * @author Paul King */public abstract class Closure<V> extends GroovyObjectSupport implements Cloneable, Runnable, GroovyCallable<V>, Serializable {
}
Two 、 Closure parameterTypes and maximumNumberOfParameters Member usage
stay Closure class Closure in , There are the following
2
2
2 Members :
protected Class[] parameterTypes; protected int maximumNumberOfParameters;
Class[] parameterTypes
Is a parameter type , int maximumNumberOfParameters
Is the maximum number of parameters required , When invoking closures , When you don't know what type of parameters to pass to a closure , Do not know how many parameters are passed , You can use Above
2
2
2 Members ;
The following code , Define a closure :
// Define closure // The closure receives 2 Parameters def closure = {
int a, String b ->}
If you use closure Closure time , I don't know what type of parameters the closure receives , And the number of parameters , have access to println Print closure parameters ;
Print parameter type : Above
2
2
2 Parameters , The printed result is [int, class java.lang.String] ;
// Print parameter type println closure.parameterTypes
Number of print parameters :
// Number of print parameters println closure.maximumNumberOfParameters
3、 ... and 、 Complete code example
Complete code example :
// Define closure // The closure receives 2 Parameters def closure = {
int a, String b ->}// Print parameter type println closure.parameterTypes// Number of print parameters println closure.maximumNumberOfParameters
Execution results :
[int, class java.lang.String]2
边栏推荐
- Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics
- A survey of automatic speech recognition (ASR) research
- [goweb development] Introduction to authentication modes based on cookies, sessions and JWT tokens
- 次小生成树
- 电源管理总线 (PMBus)
- Aperçu en direct | Services de conteneurs ACK flexible Prediction Best Practices
- Machine learning -- neural network
- NetSetMan pro (IP fast switching tool) official Chinese version v5.1.0 | computer IP switching software download
- [phantom engine UE] realize the animation production of mapping tripod deployment
- Leetcode hot topic Hot 100 day 33: "subset"
猜你喜欢
【thingsboard】替换首页logo的方法
C26451: arithmetic overflow: use the operator * on a 4-byte value, and then convert the result to an 8-byte value. To avoid overflow, cast the value to wide type before calling the operator * (io.2)
线上故障突突突?如何紧急诊断、排查与恢复
The 22nd Spring Festival Gala, an immersive stage for the yuan universe to shine into reality
如何优雅的获取每个分组的前几条数据
windows下Redis-cluster集群搭建
Qt蓝牙:搜索蓝牙设备的类——QBluetoothDeviceDiscoveryAgent
Discussion on the dimension of confrontation subspace
自动语音识别(ASR)研究综述
[PCL self study: feature9] global aligned spatial distribution (GASD) descriptor (continuously updated)
随机推荐
Neural network and deep learning Chapter 1: introduction reading questions
函数(基本:参数,返回值)
防护电路中的元器件
flutter 对象和列表
[uniapp] system hot update implementation ideas
可观测|时序数据降采样在Prometheus实践复盘
Solution of circular dependency
Construction d'un Cluster redis sous Windows
这是一个不确定的时代
CSDN body auto generate directory
蛇形矩阵
MySQL in-depth learning - index creation and deletion, index design principles, index failure scenarios, query optimization, index push down ICP
Neural networks and deep learning Chapter 3: linear model reading questions
程序员应该怎么学数学
How to remove installed elpa package
Machine learning decision tree
Discussion on the dimension of confrontation subspace
Data security -- 14 -- Analysis of privacy protection governance
PHP读取ini文件并修改内容写入
File upload bypass summary (upload labs 21 customs clearance tutorial attached)