当前位置:网站首页>[error record] groovy function parameter dynamic type error (guess: groovy.lang.missingmethodexception: no signature of method)
[error record] groovy function parameter dynamic type error (guess: groovy.lang.missingmethodexception: no signature of method)
2022-07-05 22:18:00 【Programmer community】
One 、 Error message
Definition Groovy function ,
void fun(object) {
object.hello()}If the incoming Instance object in , No definition hello Method , This will result in the following error reports ;
Error code :
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())Error message :
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:20) at Groovy$fun.callCurrent(Unknown Source) at Groovy.run(Groovy.groovy:28)
Two 、 Solution
have access to respondsTo Method , Determine whether hello function ;
void fun(object) {
if (object.respondsTo("hello")) {
object.hello() }}Also can reference 【Groovy】Groovy Dynamic language features ( Groovy Automatic type inference of function arguments in | Precautions for function dynamic parameters ) Blog , At the expense of dynamic characteristics , Limit it to static languages , Then the above runtime error will not occur ;
The complete code is as follows :
class Student {
def hello(){
println "Hello Student" }}class Worker {
def hello(){
println "Hello Worker" }}class Farmer {
}void fun(object) {
if (object.respondsTo("hello")) {
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边栏推荐
- Promql demo service
- Some tutorials install the database on ubantu so as not to occupy computer memory?
- C language knowledge points link
- 1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
- 如何快速体验OneOS
- Create a virtual machine on VMware (system not installed)
- How to add new fields to mongodb with code (all)
- Technology cloud report: how many hurdles does the computing power network need to cross?
- Pinctrl subsystem and GPIO subsystem
- ESP32 hosted
猜你喜欢

Metaverse Ape获Negentropy Capital种子轮融资350万美元

每日刷题记录 (十四)

Serializability of concurrent scheduling

The statistics of leetcode simple question is the public string that has appeared once

A trip to Suzhou during the Dragon Boat Festival holiday

U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程

Damn, window in ie open()

Database recovery strategy

Technology cloud report: how many hurdles does the computing power network need to cross?

Leetcode simple question check whether all characters appear the same number of times
随机推荐
Oracle triggers
Solutions for unexplained downtime of MySQL services
AD637使用笔记
ESP32 hosted
AD637 usage notes
Pinctrl subsystem and GPIO subsystem
Calculation method of boundary IOU
Pl/sql basic case
The American Championship is about to start. Are you ready?
What about data leakage? " Watson k'7 moves to eliminate security threats
Web3为互联网带来了哪些改变?
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
CA certificate trampled pit
A substring with a length of three and different characters in the leetcode simple question
Serializability of concurrent scheduling
Livelocks and deadlocks of concurrency control
Oracle views the data size of a table
Go语言学习教程(十五)
Leetcode simple question check whether all characters appear the same number of times