Java类类getDeclaredMethod()方法及示例

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

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

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

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

    getDeclaredMethod()方法用于返回Method对象,这些对象指示给定的类的声明方法或由此Class对象表示的接口。

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

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

  • getDeclaredMethod() method accepts two arguments, the first argument is of "String" type and the second argument is an array of "Class" type.

    getDeclaredMethod()方法接受两个参数,第一个参数为“字符串”类型,第二个参数为“类”类型的数组。

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

    getDeclaredMethod()方法在返回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 getDeclaredMethod (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 –表示“类”类型的参数数组。

Return value:

返回值:

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

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

Example:

例:

// Java program to demonstrate the example of Method 
// getDeclaredMethod (String method_name, Class ...paramType)
// method of Class 
import java.lang.reflect.*;
public class GetDeclaredMethodOfClass {
public static void main(String[] args) throws Exception {
String str = new String();
GetDeclaredMethodOfClass dc = new GetDeclaredMethodOfClass();
// Get Class object of String
Class cl = str.getClass();
// Get Class object of GetDeclaredMethodOfClass
Class dm = dc.getClass();
// Calling No argument Method
Method no_argument_method = cl.getDeclaredMethod("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.getDeclaredMethod("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 GetDeclaredMethodOfClass.argumentMethod(java.lang.Integer,java.lang.Float)

翻译自: https://www.includehelp.com/java/class-class-getdeclaredmethod-method-with-example.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/545537.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MySQL 常见的开放性问题

有一个超级大表,如何优化分页查询? 超级大表的分页优化分有以下两种方式: 数据库层面优化:利用子查询优化超多分页场景,比如:SELECT a.* FROM 表 1 a, (select id from 表 1 where 条件 LIMIT 100000,20 ) b where a.id=b.id ,先快速定位需要获取的 id 段,然后再关联查…

C语言画图形(图形库graphics的使用)

目录 工具 c语言基本绘图 文字输出 c语言基本贴图 获取鼠标、键盘信息 工具 (1)环境:VC (2)库函数:graphics.h(因为不是标准库函数,所以需下载EASYX) (3&am…

关于DDD中Domain的思考

2019独角兽企业重金招聘Python工程师标准>>> 本文既不推销UML,也不推广DDD,更不涉及各种论战。-- 作者 某天又一次打开关于DDD(领域驱动设计)的PDF文档时,自己有了个疑问:什么是领域(Domain)?译文中是这样描…

Oracle plsql 计算日期间工作日天数

declarev_fdate date : to_date(2013-10-4,yyyy-mm-dd);v_ldate date : to_date(2013-10-22,yyyy-mm-dd);v_interval_a number;v_interval_b number;v_interval_all number;begin--先算整周有几周,然后乘一周五天工作日v_interval_a : floor((v_ldate - v_fdate)/7)…

MySQL 性能优化 分布式

MySQL 性能指标都有哪些?如何得到这些指标? MySQL 的性能指标如下: ① TPS(Transaction Per Second) 每秒事务数,即数据库每秒执行的事务数。 MySQL 本身没有直接提供 TPS 参数值,如果我们想要获得 TPS 的值,只有我们自己计算了,可以根据 MySQL 数据库提供的状态变…

Java Collections CheckedCollection()方法与示例

集合类的checkedCollection()方法 (Collections Class checkedCollection() method) checkedCollection() Method is available in java.lang package. DrawnCollection()方法在java.lang包中可用。 checkedCollection() Method is used to return the typesafe view of the gi…

算法设计TSP问题动态规划

#include <iostream> #include <cmath> using namespace std; //集合虚拟化用000 、001 、010 、011 、100 、101 、110 、111分别表示{} 、{1}&#xff08;V[2^(1-1)]&#xff09; 、{2}&#xff08;V[2^(2-1)]&#xff09; 、{1,2}&#xff08;V[2^(1-1)2^(2-1)]…

Oracle 练习题 20131021 for 循环练习

--Oracle 练习题 20131021 for 循环练习--1、用for循环实现一个倒置的乘法表。beginfor i in reverse 1 .. 9 loopfor j in reverse 1 .. i loopdbms_output.put(i || x || j || || i * j || );end loop;dbms_output.put_line();end loop;end;--2、打印1-100之间所有的素数。…

字符串使用与内部实现原理

Redis 发展到现在已经有 9 种数据类型了&#xff0c;其中最基础、最常用的数据类型有 5 种&#xff0c;它们分别是&#xff1a;字符串类型、列表类型、哈希表类型、集合类型、有序集合类型&#xff0c;而在这 5 种数据类型中最常用的是字符串类型&#xff0c;所以本文我们先从字…

Lisp-Stat翻译 —— 第九章 统计绘图窗体

2019独角兽企业重金招聘Python工程师标准>>> 第九章 统计绘图窗体 除了前几章略述的绘图窗体原型提供的基本绘图工具之外&#xff0c;Lisp-Stat里的统计绘图还需要用来管理数据和将那些数据转换成屏幕上的图形的工具集。这些工具由绘图原型graph-proto提供。更多的…

Java ClassLoader getSystemResource()方法与示例

ClassLoader类getSystemResource()方法 (ClassLoader Class getSystemResource() method) getSystemResource() method is available in java.lang package. getSystemResource()方法在java.lang包中可用。 getSystemResource() method is used to find the system resource of…

操作系统(王道笔记第三章内存)

第三章内存3.1_1内存的基础知识&#xff08;1&#xff09;什么是内存&#xff1a;略&#xff08;2&#xff09;进程运行的基本原理①从写程序到程序运行②链接③装入3.1_2内存管理的概念&#xff08;1&#xff09;内存管理管哪几个方面&#xff08;2&#xff09;内存保护①上下…

按钮button加超链接

突然想做一个普通按钮&#xff0c;然后让按钮超链接到别的页面&#xff0c;不知道怎么搞&#xff0c;不过还是在百度找到了。1.如果让本页转向新的页面则用&#xff1a;<input typebutton οnclick"window.location.href(连接)"> 2.如果需要打开一个新的页面进…

Redis 快速搭建与使用

Redis 是由 C 语言开发的开源内存数据存储器&#xff0c;经常被用作数据库、缓存以及消息队列等。 Redis 因为其强大的功能和简洁的设计&#xff0c;深受广大开发者和公司的喜爱&#xff0c;几乎占领了内存数据库市场的所有份额。 1 Redis 特性 Redis 有很多优秀的特性&#…

如何计算Python中列表项的出现次数?

The python count() method counts the number of occurrences of an element in the list and returns it. python count()方法对列表中某个元素的出现次数进行计数并返回它。 Syntax: 句法&#xff1a; list.count(x)The count() method takes a single argument, x, whose…

网页自动关闭的代码

<script language"javascript">window.openernull; window.open(, _self, );//模拟自己打开自己window.close();//再关闭就没有对话框了</script>--------------------------以下是过多少秒自动关闭网页,弹窗<html><head><title>JS定时…

hibernate annotation注解方式来处理映射关系

2019独角兽企业重金招聘Python工程师标准>>> 在hibernate中&#xff0c;通常配置对象关系映射关系有两种&#xff0c;一种是基于xml的方式&#xff0c;另一种是基于annotation的注解方式&#xff0c;熟话说&#xff0c;萝卜青菜&#xff0c;可有所爱&#xff0c;每个…

操作系统Ubuntu(实验一二)

摘录&#xff1a;https://www.cnblogs.com/penglang14/p/10632360.html 实验一二1.1_小技巧1.2_ls查看目录命令1.3_cd切换目录命令1.4_查看文件内容命令(1)cat filename(2)more filename(3)head [-n] filename([]表示此内容可有可无)&#xff08;4&#xff09;wc命令确定行数、…

Redis 是如何执行的?

在以往的面试中&#xff0c;当问到一些面试者&#xff1a;Redis 是如何执行的&#xff1f;收到的答案往往是&#xff1a;客户端发命令给服务器端&#xff0c;服务端收到执行之后再返回给客户端。然而对于执行细节却「避而不谈」 &#xff0c;当继续追问服务器端是如何执行的&am…

java 枚举迭代_Java中的枚举和迭代器之间的区别

java 枚举迭代Java中的枚举与迭代器 (Enumeration vs Iterator in Java) Here, we will see how Enumeration differs from Iterator? 在这里&#xff0c;我们将看到Enumeration与Iterator有何不同&#xff1f; 1)枚举 (1) Enumeration) Enumeration is an interface which i…