博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java类类getMethod()方法及示例
阅读量:2530 次
发布时间:2019-05-11

本文共 2901 字,大约阅读时间需要 9 分钟。

类的类getMethod()方法 (Class class getMethod() method)

  • getMethod() method is available in java.lang package.

    getMethod()方法在java.lang包中可用。

  • getMethod() method is used to return Method objects that indicate the given public method of the class or an interface denoted by this Class object.

    getMethod()方法用于返回表示类的给定公共方法或由此Class对象表示的接口的Method对象。

  • getMethod() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    getMethod()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • getMethod() method may throw an exception at the time of returning a Method object.

    getMethod()方法在返回Method对象时可能会引发异常。

    • NoSuchMethodException: In this exception when a specifying method does not exist.NoSuchMethodException :在此异常中,当指定方法不存在时。
    • SecurityException: In this exception, it may raise when the security manager exists.SecurityException :在此异常中,当安全管理器存在时可能会引发此异常。
    • NullPointerException: In this exception when the given Method name is null.NullPointerException :在此异常中,当给定的Method名称为null时。

Syntax:

句法:

public Method getMethod (String method_name, Class ...paramType);

Parameter(s):

参数:

  • String method_name – represents the name of the method.

    字符串method_name –表示方法的名称。

  • Class ...paramType – represents the parameter array of Class type.

    Class ... paramType –表示Class类型的参数数组。

Return value:

返回值:

The return type of this method is Method, it returns the Method object of this Class meets the given method_name and parameter array paramType.

此方法的返回类型为Method ,它返回满足给定method_name和参数数组paramType的该Class的Method对象。

Example:

例:

// Java program to demonstrate the example // of Method getMethod (String method_name, Class ...paramType) // method of Class import java.lang.reflect.*;public class GetMethodOfClass {
public static void main(String[] args) throws Exception {
String str = new String(); GetMethodOfClass dc = new GetMethodOfClass(); // Get Class object of String Class cl = str.getClass(); // Get Class object of GetMethodOfClass Class dm = dc.getClass(); // Calling No argument Method Method no_argument_method = cl.getMethod("length", null); System.out.println(" String Method = " + no_argument_method.toString()); Class[] method_arguments = new Class[2]; method_arguments[0] = Integer.class; method_arguments[1] = Float.class; // Calling argument Method Method argument_method = dm.getMethod("argumentMethod: ", method_arguments); System.out.println("This Class Method = " + argument_method.toString()); } public void argumentMethod(Integer i, Float f) {
this.i = i; this.f = f; } public int i = 10; private float f = 10.2f;}

Output

输出量

String Method = public int java.lang.String.length()This Class Method = public void GetMethodOfClass.argumentMethod(java.lang.Integer,java.lang.Float)

翻译自:

转载地址:http://uvvzd.baihongyu.com/

你可能感兴趣的文章
SQL2043N 与 linux的randomize_va_space特性
查看>>
树莓派使用无线网卡上网相关命令
查看>>
优秀架构师是怎么炼成的?
查看>>
Hibernate的CRUD
查看>>
StringBuilder和StringBuffer的区别
查看>>
基于Extjs+SpringMVC+MyBatis+Oracle的B/S信息系统简化开发思路
查看>>
【python】字典的嵌套
查看>>
微信运营:必须收藏的101条万能微信标题公式
查看>>
【XLL 框架库函数】 TempMissing/TempMissing12
查看>>
利用VS自带发布功能实现web项目快速部署
查看>>
第七讲 数组动手动脑和课后作业
查看>>
zencart 对首页静态化处理
查看>>
50多条mysql数据库优化建议
查看>>
crontab 详细用法 定时任务
查看>>
配置流行为
查看>>
js数组的迭代
查看>>
Maven系列--"maven-compiler-plugin"的使用、Maven之Surefire插件
查看>>
20165202 实验一 Java开发环境的熟悉
查看>>
Linux篇---Grep和正则匹配
查看>>
搭建SSM项目时报错(org.springframework.jdbc.CannotGetJdbcConnectionException)
查看>>