JAVA反射机制入门(二)--getMethods()系列方法特例详解

本文详细探讨了Java反射中的getMethods()方法在面对同名同参但返回类型不同的情况时的工作原理,揭示了Java语言与JVM对重载的不同理解。文中通过示例介绍了协变返回类型和桥方法的概念,解释了编译器如何处理这些特殊情况,并展示了如何在运行时通过反射获取特定方法。
摘要由CSDN通过智能技术生成

 昨天在研究getDeclaredMethod(String name,Class<?>... parameterTypes)API时,发现这么一段话:

The name parameter is a String that specifies the simple name of the desired method, and the parameterTypes parameter is an array of Class objects that identify the method's formal parameter types, in declared order. If more than one method with the same parameter types is declared in a class, and one of these methods has a return type that is more specific than any of the others, that method is returned; otherwise one of the methods is chosen arbitrarily. If the name is "<init>"or "<clinit>" a NoSuchMethodException is raised.

 

就此产生了疑问,如果指定了方法的名字和参数的类型,为什么还可能会得到多个方法,java中不是不允许同名同参的overloaded吗?接着在getMethod()的API中找到了答案:

Note that there may be more than one matching method in a class because while the Java language forbids a class to declare multiple methods with the same signature but different return types, the Java virtual machine does not.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值