package com.swift;

import java.util.*;
import java.lang.reflect.*; public class ReflectDemo {
public static void main(String[] args) throws Exception {
ArrayList<String> list = new ArrayList<String>();
list.add("enen");
for(String str:list) {
System.out.println(str);
} Class<? extends ArrayList> clas=list.getClass();
ArrayList<String> li=(ArrayList<String>) clas.newInstance();
li.add("san");
for(String str:li) {
System.out.println(str);
} Method[] metho=clas.getMethods();
for(Method met:metho) {
System.out.println(met);
}
Method meth=clas.getMethod("add",Object.class);//后边的.class 是add方法的参数的类型类
meth.invoke(li, "123");
for(String str:li) {
System.out.println(str);
} Class<?> c=Class.forName("com.swift.ADemo");
ADemo a=(ADemo) c.newInstance();
a.fun(); Class cl=a.getClass();
ADemo b=(ADemo) cl.newInstance();
b.fun();
Method met=cl.getMethod("fun");
met.invoke(b); Method[] m=c.getDeclaredMethods();
for(Method me:m) {
System.out.println(me);
} Method method=c.getMethod("fun");
method.invoke(a); }
} class ADemo{
public void fun() {
System.out.println("A demo.");
}
}

反射中 invoke方法 getMethod方法 getClass()方法的更多相关文章

  1. JAVA反射中的getFields()方法和getDeclaredFields ()方法的区别

    JAVA反射中的getFields()方法和getDeclaredFields ()方法的区别   关于获取类的字段有两种方式:getFields()和getDeclaredFields().我们先来 ...

  2. [转]JAVA反射中的getFields()方法和getDeclaredFields ()方法的区别

    关于获取类的字段有两种方式:getFields()和getDeclaredFields().我们先来看看这两者的区别吧: getFields():获得某个类的所有的公共(public)的字段,包括父类 ...

  3. java反射中Method类invoke方法的使用方法

    package com.zsw.test; import java.lang.reflect.Method;import java.lang.reflect.InvocationTargetExcep ...

  4. Java反射中的getClass()方法

    Java反射学习 所谓反射,可以理解为在运行时期获取对象类型信息的操作.传统的编程方法要求程序员在编译阶段决定使用的类型,但是在反射的帮助下,编程人员可以动态获取这些信息,从而编写更加具有可移植性的代 ...

  5. Java反射中method.isBridge() 桥接方法

    桥接方法是 JDK 1.5 引入泛型后,为了使Java的泛型方法生成的字节码和 1.5 版本前的字节码相兼容,由编译器自动生成的方法.我们可以通过Method.isBridge()方法来判断一个方法是 ...

  6. java 反射机制之 getDeclaredMethod()获取方法,然后invoke执行实例对应的方法

    关于反射中getDeclaredMethod().invoke()的学习,来源于项目中的一行代码: SubjectService.class.getDeclaredMethod(autoMatchCo ...

  7. super.getClass()方法调用

    下面程序的输出结果是多少?import java.util.Date;public class Test extends Date{public static void main(String[] a ...

  8. super.getClass()方法

    下面程序的输出结果是多少? importjava.util.Date; public class Test extends Date{ public static void main(String[] ...

  9. Java基础知识强化26:Object类之hashCode()方法、getClass()方法

    1. Object类的hashCode()方法,如下: public  int  hashCode():返回该对象的哈希码值,这个值和地址值有关,但是不是实际地址值(哈希码值是根据实际地址值转化过来的 ...

随机推荐

  1. 洛谷P3293 [SCOI2016]美味(主席树)

    传送门 据说这题做法叫做可持久化trie树?(然而我并不会) 首先考虑一下贪心,从高位到低位枚举,如果能选1肯定比选0优 假设已经处理到了$b$的第$i$位,为1(为0的话同理就不说了) 那么只有当$ ...

  2. SpringBoot(1)—启动原理之SpringApplication对象的创建

    创建SpringApplication对象 SpringBoot版本为 2.1.1.RELEASE @SpringBootApplication public class SpringbootDemo ...

  3. 3分钟简单了解 prototype 和 __proto__

    关于prototype 1. 所有的函数都会有一个prototype属性,属性值是一个普通对象: 2. 当我们去new一个构造函数的实例时,构造函数的原型对象(prototype)会被赋值给它实例的[ ...

  4. 【ZROI 537】贪心题 题解

    [ZROI 537]贪心题 题解 Link Solution 最大的一边直接放到一起贪心即可 着重讲小的一边 已知对于二分图匹配,其答案即为最大流 令时间集合为 \(T = {1,2,3,\dots, ...

  5. Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) B

    Arpa is taking a geometry exam. Here is the last problem of the exam. You are given three points a,  ...

  6. 1081 Rational Sum(20 分)

    Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...

  7. 线程池(1)ThreadPoolExecutor梳理

    使用默认的 thread factory创建ThreadPoolExecutor实例 public ThreadPoolExecutor(int corePoolSize, int maximumPo ...

  8. yum指令之修复

    折腾着搞 openvpn 网站服务器 yum指令 出了点问题 ------------------------------------------------------------ [root@cl ...

  9. Emacs中自动刷新dired缓冲区

    Emacs中自动刷新dired缓冲区 在dired模式中,如果在不同buffer间切换,buffer不会自动更新,有时还需要手工按“g”键,比较麻烦,如下设置和代码能够在buffer切换和执行shel ...

  10. DetachedCriteria的简单使用

    一. DetachedCriteria使得hibernate能够对查询条件进行面向对象的方式来组装.其创建方式有两种: 1.1直接用class创建:DetachedCriteria criteria  ...