1、

package reflectionZ;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List; public class Treflection02
{
public static void main(String[] args) throws Exception
{
// 第15课
// getMethods()、getMethod() Class<?> clazz1 = Class.forName("reflectionZ.Cat"); // 通过Class对象来得到构造函数
Constructor<?> c2 = clazz1.getConstructor(String[].class);
String[] foods = {"鱼", "老鼠"};
//Cat cat2 = (Cat)c2.newInstance((Object)foods); // 强转
//cat2.Show(); Object obj = c2.newInstance((Object)foods);
/*
// 使用反射来调用 Show()
// 获取 Show()方法
// (1)、无参数
Method method = clazz1.getMethod("Show");
method.invoke(obj);
//*/
/*
// (2)、无参数
Method method = clazz1.getMethod("Show", null);
method.invoke(obj, null);
//*/
/*
// (3)、一个String参数
Method method = clazz1.getMethod("Show", Class.forName("java.lang.String"));
method.invoke(obj, "AA");
//*/
/*
// (4)、两个参数: String + int
Method method = clazz1.getMethod("Show", Class.forName("java.lang.String"));
method.invoke(obj, "BB", 2);
//*/
/*
// (5)、一个 List类型的参数
Method method = clazz1.getMethod("Show", List.class);
List list = new ArrayList();
list.add("A01");
list.add("A02");
list.add("A03");
method.invoke(obj, list);
//*/
//*
// (6)、一个参数: int
// 调用私有的函数
Method method = clazz1.getDeclaredMethod("Show", int.class);
method.setAccessible(true); // 暴力访问
method.invoke(obj, 5);
//*/
}
}

2、

Treflection02_getMethods()_getMethod()的更多相关文章

随机推荐

  1. crash处理core文件

    (一时心血来潮总结的,供大家参考,时间仓促,不足之处勿拍砖,欢迎讨论~)Crash工具用于解析Vmcore文件,Vmcore文件为通过kdump等手段收集的操作系统core dump信息,在不采用压缩 ...

  2. powershell Start-Sleep

    秒: Start-Sleep –s 10 ,毫秒) Start-Sleep –m 10000 语法 Start-Sleep [-seconds] <int> [<CommonPara ...

  3. [转载]js复制内容加版权声明代码

    转自:https://www.cnblogs.com/zdz8207/p/js-oncopy.html var ua = navigator.userAgent.toLowerCase(); if( ...

  4. Java 语言基础之数组应用

    什么时候使用数组呢? 如果数据出现了对应关系, 而且对应关系的一方是有序的数字编号, 并作为角标使用. 这时,就必须要想到数组的使用. 也就是将这些数据存储到数组中, 根据运算的结果作为角标, 直接去 ...

  5. 【vim使用】

    nano,与vim相似的一个文本编辑工具,在git merge时默认使用 https://www.vpser.net/manage/nano.html 这里介绍一下如何退出nano 按Ctrl+X 如 ...

  6. 洛谷 P2233 [HNOI]公交车线路

    洛谷 不知道大家做没做过传球游戏,这一题和传球游戏的转移方程几乎一样. 令\(A\)为\(1\)点,\(E\)为\(5\)点,那么\(f[i][j]\)代表第i步走到j的方案数. \[f[i][j]= ...

  7. Selenium定位不到指定元素原因之iframe(unable to locate element)

    浏览过程中,图片中的内容可能太小,无法看清,可以>右键>在新标签中打开 Outline 项目原因,需要用selenium实现模拟登陆.模拟上传文件,自然就需要模拟点击[上传]按钮: 模拟点 ...

  8. for and range()

    pyhon 中 for 循环可以遍历任何序列的项目,如一个字典或者一个字符. for 循环格式一般如下: for <variable-变量> in <sequence-序列>: ...

  9. JsonResponse对象浅析

    JsonResponse   JsonResponse 对象: class JsonResponse(data, encoder=DjangoJSONEncoder, safe=True, json_ ...

  10. 在Docker上安装配置Oracle教程

    地址:https://github.com/wnameless/docker-oracle-xe-11g Docker shell 下: docker pull wnameless/oracle-xe ...