当前位置:网站首页>[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
边栏推荐
- AD637 usage notes
- How to quickly experience oneos
- Serializability of concurrent scheduling
- Create a virtual machine on VMware (system not installed)
- Server optimization of performance tuning methodology
- MySQL actual combat 45 lecture learning (I)
- Interprocess communication in the "Chris Richardson microservice series" microservice architecture
- Leetcode simple question: check whether each row and column contain all integers
- Oracle is sorted by creation time. If the creation time is empty, the record is placed last
- Calculation method of boundary IOU
猜你喜欢
随机推荐
Decorator learning 01
Business learning of mall order module
Alternating merging strings of leetcode simple questions
PyGame practical project: write Snake games with 300 lines of code
科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖
Leetcode simple question: find the nearest point with the same X or Y coordinate
What changes has Web3 brought to the Internet?
Leetcode simple question: the minimum cost of buying candy at a discount
Promql demo service
ICMP introduction
The simple problem of leetcode is to split a string into several groups of length K
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
Storage optimization of performance tuning methodology
Text组件新增内容通过tag_config设置前景色、背景色
Go语言学习教程(十五)
实战:fabric 用户证书吊销操作流程
Search: Future Vision (moving sword)
阿龙的感悟
Comment développer un plug - in d'applet






![Sparse array [matrix]](/img/62/27b02deeeaa5028a16219ef51ccf82.jpg)

