- python中eof表示什么语句错误
- python中for语句涉及的序列
- python中if是循环语句吗
- python中if语句与或非
要
动态获取一个对象方法的信息,首先需要通过下列方法之一创建一个
Method
类型的对象或者数组。
objectClass.getDeclaredConstructor("max",int.class,String.class); objectClass.getDeclaredConstructor("max",new Class[]{int.class,String.class});
静态方法名称 | 说明 |
---|---|
getName() | 获取该方法的名称 |
getParameterType() | 按照声明顺序以 Class 数组的形式返回该方法各个参数的类型 |
getReturnType() | 以 Class 对象的形式获得该方法的返回值类型 |
getExceptionTypes() | 以 Class 数组的形式获得该方法可能抛出的异常类型 |
invoke(Object obj,Object...args) | 利用 args 参数执行指定对象 obj 中的该方法,返回值为 Object 类型 |
isVarArgs() | 查看该方法是否允许带有可变数量的参数,如果允许返回 true,否则返回 false |
getModifiers() | 获得可以解析出该方法所采用修饰符的整数 |
public class Book1 { // static 作用域方法 static void staticMethod() { System.out.println("执行staticMethod()方法"); } // public 作用域方法 public int publicMethod(int i) { System.out.println("执行publicMethod()方法"); return 100 + i; } // protected 作用域方法 protected int protectedMethod(String s, int i) throws NumberFormatException { System.out.println("执行protectedMethod()方法"); return Integer.valueOf(s) + i; } // private 作用域方法 private String privateMethod(String... strings) { System.out.println("执行privateMethod()方法"); StringBuffer sb = new StringBuffer(); for (int i = 0; i < sb.length(); i++) { sb.append(strings[i]); } return sb.toString(); } }2)编写测试类 Test02,在该类的 main() 方法中通过反射访问 Book1 类中的所有方法,并将该方法是否带可变类型参数、入口参数类型和可能拋出的异常类型信息输出到控制台。
public class Test02 { public static void main(String[] args) { // 获取动态类Book1 Book1 book = new Book1(); Class class1 = book.getClass(); // 获取Book1类的所有方法 Method[] declaredMethods = class1.getDeclaredMethods(); for (int i = 0; i < declaredMethods.length; i++) { Method method = declaredMethods[i]; System.out.println("方法名称为:" + method.getName()); System.out.println("方法是否带有可变数量的参数:" + method.isVarArgs()); System.out.println("方法的参数类型依次为:"); // 获取所有参数类型 Class[] methodType = method.getParameterTypes(); for (int j = 0; j < methodType.length; j++) { System.out.println(" " + methodType[j]); } // 获取返回值类型 System.out.println("方法的返回值类型为:" + method.getReturnType()); System.out.println("方法可能抛出的异常类型有:"); // 获取所有可能抛出的异常 Class[] methodExceptions = method.getExceptionTypes(); for (int j = 0; j < methodExceptions.length; j++) { System.out.println(" " + methodExceptions[j]); } boolean isTurn = true; while (isTurn) { try { // 如果该成员变量的访问权限为private,则抛出异常 isTurn = false; if (method.getName().equals("staticMethod")) { // 调用没有参数的方法 method.invoke(book); } else if (method.getName().equals("publicMethod")) { // 调用一个参数的方法 System.out.println("publicMethod(10)的返回值为:" + method.invoke(book, 10)); } else if (method.getName().equals("protectedMethod")) { // 调用两个参数的方法 System.out.println("protectedMethod(\"10\",15)的返回值为:" + method.invoke(book, "10", 15)); } else if (method.getName().equals("privateMethod")) { // 调用可变数量参数的方法 Object[] parameters = new Object[] { new String[] { "J", "A", "V", "A" } }; System.out.println("privateMethod()的返回值为:" + method.invoke(book, parameters)); } } catch (Exception e) { System.out.println("在设置成员变量值时抛出异常,下面执行setAccessible()方法"); method.setAccessible(true); // 设置为允许访问private方法 isTurn = true; } } System.out.println("=============================\n"); } } }3)运行测试类 test02,程序将会依次动态访问 Book1 类中的所有方法。访问 staticMethod() 方法的运行效果如下所示:
方法名称为:staticMethod 方法是否带有可变数量的参数:false 方法的参数类型依次为: 方法的返回值类型为:void 方法可能抛出的异常类型有: 执行staticMethod()方法 =============================
方法名称为:publicMethod 方法是否带有可变数量的参数:false 方法的参数类型依次为: int 方法的返回值类型为:int 方法可能抛出的异常类型有: 执行publicMethod()方法 publicMethod(10)的返回值为:110 =============================
方法名称为:protectedMethod 方法是否带有可变数量的参数:false 方法的参数类型依次为: class java.lang.String int 方法的返回值类型为:int 方法可能抛出的异常类型有: class java.lang.NumberFormatException 执行protectedMethod()方法 protectedMethod("10",15)的返回值为:25 =============================
方法名称为:privateMethod 方法是否带有可变数量的参数:true 方法的参数类型依次为: class java.lang.String; 方法的返回值类型为:class java.lang.String 方法可能抛出的异常类型有: 在设置成员变量值时抛出异常,下面执行setAccessible()方法 执行privateMethod()方法 privateMethod()的返回值为: =============================
Hive —— 入门 Hive介绍 Apache Hive是一款建立在Hadoop之上的开源数据仓库系统,可以将存储在Hadoop文件中的结构化、半结构化数据文件映射为一张数据库表,基于表提供了一
HBase —— 入门 HBase介绍 HBase是一个分布式的、面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文“Bigtable:一个结构化数据的分布式存储系统”
零:前端目前形势 前端的发展史 HTML(5)、CSS(3)、JavaScript(ES5、ES6):编写一个个的页面 -> 给后端(PHP、Python、Go、Java) ->
在本教程中,您将了解在计算机上运行 JavaScript 的不同方法。 JavaScript 是一种流行的编程语言,具有广泛的应用程序。 JavaScript 以前主要用于使网页具有交
我曾经是一个对编程一窍不通的小白,但因为对互联网世界的好奇心和求知欲的驱使,我踏入了编程的殿堂。在学习的过程中,我发现了一门神奇的编程语言——Python。Python有着简洁、易读的语法,让初学者能
嗨,亲爱的读者们! 今天我要给大家分享一些关于Python爬虫的小案例。你是否曾为了获取特定网页上的数据而烦恼过?或者是否好奇如何从网页中提取信息以供自己使用?那么,这篇文章将会给你一些启示和灵感。
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 8 年前。 Improv
我想创建一个像https://apprtc.appspot.com/?r=04188292这样的应用程序。我对 webrtc 了解一点,但无法掌握 google app-engine。如何为 java
我刚刚开始使用 Python 并编写了一个简单的周边程序。但是,每当我在终端中键入 python perimeter.py 时,都会收到以下错误,我不知道如何解决。 >>> python perime
Redis有5个基本数据结构,string、list、hash、set和zset。它们是日常开发中使用频率非常高应用最为广泛的数据结构,把这5个数据结构都吃透了,你就掌握了Redis应用知识的一半了
创建发布web项目 具体步骤: 1.在开发工具中创建一个dynamic web project helloword 2.在webContent中创建index.html文件 3.发布web应用到
如果你在 Ubuntu 上使用终端的时间很长,你可能会希望调整终端的字体和大小以获取一种良好的体验。 更改字体是一种最简单但最直观的 Linux 的终端自定义 的方法。让我
1. 前言 ADODB 是 Active Data Objects Data Base 的简称,它是一种 PHP 存取数据库的函式组件。现在 SFS3 系统 (校园自由软件交流网学务系统) 计划的
我对 neo4j 完全陌生,我很抱歉提出这样一个基本问题。我已经安装了neo4j,我正在使用shell“localhost:7474/webadmin/#/console/” 我正在寻找一个很好的例子
我正在阅读 ios 4 的核心音频,目的是构建一个小测试应用程序。 在这一点上,我对所有 api 的研究感到非常困惑。理想情况下,我想知道如何从两个 mp3 中提取一些样本到数组中。 然后在回调循环中
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是无关紧要的,因
我下载了 GNUStep并安装了它,但是我不确定在哪里可以找到 IDE。有谁知道什么程序可以用作 GNUStep IDE/从哪里获取它们?否则,有没有人知道有关如何创建和编译基本 GNUStep 程序
我正在尝试开始使用 Apache Solr,但有些事情我不清楚。通读tutorial ,我已经设置了一个正在运行的 Solr 实例。我感到困惑的是 Solr 的所有配置(架构等)都是 XML 格式的。
请问有没有关于如何开始使用 BruTile 的文档? 我目前正在使用 SharpMap,我需要预缓存切片以加快进程 最佳答案 我今天正在研究这个:)Mapsui项目site严重依赖 SharpMap
尽我所能,我无法让 CEDET 做任何事情。 Emacs 24.3。我下载了最新的 CEDET 快照。我从他的底部(不是这样)Gentle Introduction 中获取了 Alex Ott 的设置
我是一名优秀的程序员,十分优秀!