首页 > Java > java教程 > 正文

Java getMethod()

PHPz
发布: 2024-08-30 15:39:49
原创
1032 人浏览过

Java getMethod() 是 java.lang.Class.getMethod() 中的一个方法,它返回 java.lang.reflect 包中 Method 类的实例,该实例保存给定 Class 对象引用中存在的给定公共成员函数的引用到一个类或接口。该方法将需要传递的方法的名称作为其第一个参数。要传递的第二个参数是 Class 对象数组,用于确定返回方法的形式参数数据类型,或者是一个空数组,用于确定 paramterTypes 为 null。这里使用的搜索算法与私有 GetPublicMethods() 方法相同。

广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

getMethod() 抛出 3 种类型的异常,如下所示:-

  1. NoSuchMethodException
  2. 空指针异常
  3. 安全异常

语法

下面是java.lang.Class的getMethod的签名

public Method getMethod(String name, Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException
登录后复制
  • public:该关键字确定可以从项目中的任何类访问给定的方法。
  • 返回类型方法:此方法返回 Method 类的实例,该实例引用其名称已作为参数传递的所需方法。
  • 参数:
  • Name 此参数指引用类或接口对象中存在的方法名称的字符串表示形式。如果类中不存在此类方法,则会引发 NoSuchMethodException。否则,算法运行,并返回该方法。
  • parameterTypes:这是指类类型对象的数组,该数组指向名称参数中的方法需要作为参数的数据。该数组的大小取决于指定方法名称所需的参数。如果该方法不需要参数,则将 null 传递给该参数。

示例

如果我们有一个如下所示的类演示:

class Demo{
public void method1(String a){
System.out.println(a);
}
}
登录后复制

然后调用 getMethod 就会像:

Demo demoObj= new Demo();// Object of Demo class
Class cObj= demoObj.getClass()
Class [] carr = new Class[1];
carr[0] = String.class;// class reference to java.lang.String class stored In the array of type Class
Method mObj = cObj.getMethod("method1",carr);
登录后复制

getMethod() 在 Java 中如何工作?

getMethod() 返回引用类或接口对象中指定方法的 Method 实例。

  • 它采用 String 数据类型的 name 参数,该参数保存需要在指定类或接口中查找的公共方法的名称。它还需要一个类对象数组,代表我们正在寻找的函数的参数类型。
  • JVM 读取两个参数并执行与 java.lang.Class 中的 privateGetPublicMethods() 方法相同的搜索算法,并搜索指定名称的公共方法是否存在。如果类中存在多个方法,则返回具有更具体返回类型的方法。否则,方法是任意选择的。

如果找到该方法,它会返回一个保存其引用的方法类实例。

如果指定的方法不需要任何参数,则传递 null 来代替parameterType。这在方法重载的情况下很有帮助,在这种情况下,我们有多个具有相同名称但参数数量或数据类型不同的方法。此方法会抛出 3 种类型的异常:-

1。 NoSuchMethodException:当 JVM 无法在类或接口中找到任何具有指定名称的方法时,抛出此类异常。

2。 SecurityException:当

时抛出此类异常
  • checkMemberAccess(this, Member.PUBLIC) 被调用,拒绝对其的访问。
  • 调用者类加载器与当前类的祖先类的加载器不同;因此,SecurityManagers.checkPackageAccess() 被调用;因此,对包的访问被拒绝。

3。 NullPointerException:如果传递 null 来代替参数中的方法名称,则会抛出此异常。

实现 Java getMethod() 的示例

以下是提到的示例:

示例#1

在此示例中,我们将显示对 Office 类的两个方法的 getMethod 调用的输出,一个需要对象,另一个不需要参数。

//package Proc;
import java.lang.reflect.*;
class Office{
public String OfficeLocation() {
return location;
}
public String getEmpName(Integer eid) {
return"Sergio";
}
String location = "Bangalore";
}
public class prac1 {
public static void main(String[] args) {
Office ofc = new Office();
Class cObj = ofc.getClass();
Class[] carr = new Class[1];
carr[0] = Integer.class;
try {
Method meth = cObj.getMethod("OfficeLocation", null);
System.out.println("Method with specified name is = " + meth.toString());
} catch(NoSuchMethodException e) {
System.out.println(e.toString());
}
try {
Method meth = cObj.getMethod("getEmpName", carr);
System.out.println("Method with specified name is = " + meth.toString());
} catch(NoSuchMethodException e) {
System.out.println(e.toString());
}
}
}
登录后复制

输出:

Java getMethod()

示例#2

在这个例子中,我们将看看 JVM 是否能够找到具有给定名称的类的私有方法。

//package Proc;
import java.lang.reflect.*;
public class prac1 {
public static void main(String[] args) {
Office ofc = new Office();
Class cObj = ofc.getClass();
try {
Method meth = cObj.getMethod("OfficeLocation", null);
System.out.println("Method with specified name is = " + meth.toString());
} catch(NoSuchMethodException e) {
System.out.println(e.toString());
}
}
}
class Office{
private String OfficeLocation() {
return location;
}
public String getEmpName(Integer eid) {
return "Sergio";
}
String location = "Bangalore";
}
登录后复制

输出:

Java getMethod()

Example #3

In this example , we will see how different exceptions occur when a non-existing method is called, and null is passed in the method’s name.

//package Proc;
import java.lang.reflect.*;
class Office{
public String OfficeLocation() {
return location;
}
public String getEmpName(Integer eid) {
return "Sergio";
}
String location = "Bangalore";
}
public class prac1 {
public static void main(String[] args) {
Office ofc = new Office();
Class cObj = ofc.getClass();
Class[] carr = new Class[1];
carr[0] = Integer.class;
try {
Method meth = cObj.getMethod("show", null);
System.out.println("Method found " + meth.toString());
} catch(NoSuchMethodException e) {
System.out.println(e.toString());
}
try {
Method meth = cObj.getMethod(null, carr);
System.out.println("Method found" + meth.toString());
} catch(NoSuchMethodException e) {
System.out.println(e.toString());
}catch(NullPointerException e) {
System.out.println(e.toString());
}
}
}
登录后复制

Output:

Java getMethod()

Conclusion

Java.lang.getMethod() is a method used to search if a method with the given name and type of arguments is present in the class or not. It uses the same algorithm to find the method used in the privateGetPublicMethods() method. JVM search for the given public method and returns a Method instance; otherwise, NoSuchMethodException is raised.

以上是Java getMethod()的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!