当前位置:网站首页>[groovy] groovy dynamic language features (automatic type inference of function arguments in groovy | precautions for function dynamic parameters)
[groovy] groovy dynamic language features (automatic type inference of function arguments in groovy | precautions for function dynamic parameters)
2022-07-05 22:17:00 【Programmer community】
List of articles
- Preface
- One 、Groovy Automatic type inference of function arguments in
- Two 、 Precautions for function dynamic parameters
- 3、 ... and 、 Complete code example
Preface
Groovy It's dynamic language , Java It's a static language ;
This blog discussion Groovy in , Automatic type inference of function arguments ;
One 、Groovy Automatic type inference of function arguments in
Define two different classes Student and Worker , Define in the class hello Method ;
class Student {
def hello(){
println "Hello Student" }}class Worker {
def hello(){
println "Hello Worker" }}
Declare a method , Receiving parameters object , The parameter type is not specified temporarily , Call the... Of the parameter object in the function hello Method ;
void fun(object) {
object.hello()}
Report to the fun Function passed in Student and Worker object , Then... In the corresponding class will be called respectively hello Method ;
fun(new Student())fun(new Worker())
Two 、 Precautions for function dynamic parameters
Special attention should be paid here , Don't pass the wrong object , If there is no definition in the class hello Method , It can be compiled through , But the runtime will report an error ;
Such as : Defined a no hello Class of method ,
class Farmer {
}
The instance object of this class is passed in fun Method as parameter ,
fun(new Farmer())
The following error will be reported :
Caught: groovy.lang.MissingMethodException: No signature of method: Farmer.hello() is applicable for argument types: () values: []Possible solutions: sleep(long), sleep(long, groovy.lang.Closure), getAt(java.lang.String), each(groovy.lang.Closure), split(groovy.lang.Closure), wait()groovy.lang.MissingMethodException: No signature of method: Farmer.hello() is applicable for argument types: () values: []Possible solutions: sleep(long), sleep(long, groovy.lang.Closure), getAt(java.lang.String), each(groovy.lang.Closure), split(groovy.lang.Closure), wait() at Worker$hello.call(Unknown Source) at Groovy.fun(Groovy.groovy:17) at Groovy$fun.callCurrent(Unknown Source) at Groovy.run(Groovy.groovy:22)
To avoid the above problems , You can use @TypeChecked annotation , But the corresponding , It's lost Groovy The dynamics of language ;
@TypeCheckedvoid fun(Student object) {
object.hello()}
3、 ... and 、 Complete code example
Complete code example :
class Student {
def hello(){
println "Hello Student" }}class Worker {
def hello(){
println "Hello Worker" }}class Farmer {
}void fun(object) {
object.hello()}fun(new Student())fun(new Worker())// The following usage will report Caught: groovy.lang.MissingMethodException abnormal //fun(new Farmer())
Execution results :
Hello StudentHello Worker
边栏推荐
- 装饰器学习01
- database mirroring
- A substring with a length of three and different characters in the leetcode simple question
- Matlab draws a cute fat doll
- Learning of mall permission module
- Performance monitoring of database tuning solutions
- Metaverse Ape上线倒计时,推荐活动火爆进行
- Overview of concurrency control
- Metaverse Ape获Negentropy Capital种子轮融资350万美元
- How to quickly experience oneos
猜你喜欢
MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
50. Pow(x, n). O(logN) Sol
Win11缺少dll文件怎么办?Win11系统找不到dll文件修复方法
Leetcode simple question check whether all characters appear the same number of times
Decorator learning 01
Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
How to view Apache log4j 2 remote code execution vulnerability?
2022-07-05: given an array, you want to query the maximum value in any range at any time. If it is only established according to the initial array and has not been modified in the future, the RMQ meth
Database tuning solution
Kubernetes Administrator certification (CKA) exam notes (IV)
随机推荐
Code bug correction, char is converted to int high-order symbol extension, resulting in changes in positivity and negativity and values. Int num = (int) (unsigned int) a, which will occur in older com
A long's perception
Database tuning solution
The new content of the text component can be added through the tag_ Config set foreground and background colors
元宇宙中的三大“派系”
2022软件测试工程师涨薪攻略,3年如何达到30K
Understand the basic concept of datastore in Android kotlin and why SharedPreferences should be stopped in Android
Unique occurrence times of leetcode simple questions
Database recovery strategy
Oracle triggers
Nacos installation and service registration
Promql demo service
Leetcode simple question ring and rod
Oracle advanced query
Technology cloud report: how many hurdles does the computing power network need to cross?
Performance monitoring of database tuning solutions
从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
Win11缺少dll文件怎么办?Win11系统找不到dll文件修复方法
Calculation method of boundary IOU
Pinctrl subsystem and GPIO subsystem