gpt4 book ai didi

java - Class 类中的 getDeclaredMethods()

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:38:56 25 4
gpt4 key购买 nike

public Method[] getDeclaredMethods() throws SecurityException

具有如下文档。

/**
*
* Returns an array containing {@code Method} objects reflecting all the
* declared methods of the class or interface represented by this {@code
* Class} object, including public, protected, default (package)
* access, and private methods, but excluding inherited methods.

<p> If this {@code Class} object represents a type that has multiple
* declared methods with the same name and parameter types, but different
* return types, then the returned array has a {@code Method} object for
* each such method.
*

一个类怎么能有两个[声明的]方法,它们具有不同的返回类型、相同的名称和相同的参数类型?

最佳答案

Java 语言不可能声明两个具有相同名称和参数类型但返回类型不同的方法。

然而,Java 虚拟机 (JVM) 可以毫无问题地处理这样的方法。

有一种方法可以通过使用更具体的返回类型覆盖基类方法来隐式创建此类方法。

如果一个基类有一个方法 Object foo() 而你的类用一个方法覆盖它 String foo() 那么 .class 你的类的文件将有一个合成的(因此不是“可见的”)方法 Object foo() 和一个“正常”的方法 String foo()Object foo() 方法的存在是为了与那些希望将您的类视为基类的实例并且不知道 String foo() 覆盖的类兼容.

此外,一些非 Java 语言可能允许您显式地使用这些类型的方法创建类。

关于java - Class 类中的 getDeclaredMethods(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48113697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com