package com.yjf.esupplier.common.test; import java.lang.reflect.Method; import java.util.ArrayList; /** * @author shusheng * @description 通过反射越过泛型检查 * @Email shusheng@yiji.com * @date 2019/1/5 18:37 */ public class ReflectDemo5 { public static void main(String[] args) throws Exception { // 创建集合对象 ArrayList<Integer> array= new ArrayList<Integer>(); Class c = array.getClass(); Method m1 = c.getMethod("add", int.class, Object.class); m1.invoke(array,0,"hello"); m1.invoke(array,1,"world"); m1.invoke(array,2,"java"); System.out.println(array); System.out.println("------------------------------"); Method m2 = c.getMethod("add",Object.class); m2.invoke(array,"hello"); m2.invoke(array,"world"); m2.invoke(array,"java"); System.out.println(array); } }
package com.yjf.esupplier.common.test; /** * @author shusheng * @description * @Email shusheng@yiji.com * @date 2018/12/29 13:42 */ public class Person { private String name; int age; public String address; public Person() { } private Person(String name) { this.name = name; } Person(String name, int age) { this.name = name; this.age = age; } public Person(String name, int age, String address) { this.name = name; this.age = age; this.address = address; } public void show() { System.out.println("show方法的输出"); } public void method(String s) { System.out.println("method方法的输出: " + s); } public String getString(String s, int i) { return s + "---" + i; } private void function() { System.out.println("function方法的输出"); } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", address=" + address + "]"; } }