反射构造方法
package bull03.Constructor;
public class Bean implements java.io.Serializable {
private String name;
private int age;
public Bean() {
System.out.println("调用了Bean的无参构造!");
}
public Bean(String name,int age) {
System.out.println("调用了Bean的有参构造!");
this.name = name;
this.age = age;
}
private Bean(String name) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package bull03.Constructor;
import java.lang.reflect.Constructor;
import org.junit.Test;
public class ConstructorDemo {
@Test
public void method() {
Bean be = new Bean();
}
@Test
public void method1() throws Exception {
Class clazz = Class.forName("bull03.Constructor.Bean");
Constructor cons = clazz.getConstructor();
Object obj = cons.newInstance();
System.out.println(obj);
}
@Test
public void method2() throws Exception{
Class clazz = Class.forName("bull03.Constructor.Bean");
Constructor cons = clazz.getConstructor(String.class,int.class);
Object obj = cons.newInstance("aaa",10);
System.out.println(obj);
}
@Test
public void method3() throws Exception {
Class clazz = Class.forName("bull03.Constructor.Bean");
Object obj = clazz.newInstance();
System.out.println(obj);
}
}
package bull03.Constructor;
import java.lang.reflect.Constructor;
import org.junit.Test;
public class PrivateConstructor {
@Test
public void method() throws Exception {
Class clazz = Class.forName("bull03.Constructor.Bean");
Constructor cons = clazz.getDeclaredConstructor(String.class);
cons.setAccessible(true);
Object obj = cons.newInstance("aaa");
System.out.println(obj);
}
}
反射普通方法
package bull04.Method;
import java.util.Arrays;
public class Bean implements java.io.Serializable {
private String name;
private int age;
public String sex;
public Bean() {
System.out.println("调用了Bean的无参构造!");
}
public Bean(String name,int age) {
System.out.println("调用了Bean的有参构造!");
this.name = name;
this.age = age;
}
private Bean(String name) {
this.name = name;
this.age = age;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(args));
}
private void show(int num) {
System.out.println("私有方法:"+num);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Bean [name=" + name + ", age=" + age + "]";
}
}
package bull04.Method;
import java.lang.reflect.Method;
import org.junit.Test;
public class PublicMethod {
@Test
public void method1() {
Bean be = new Bean();
be.setName("aaa");
be.setAge(10);
String name = be.getName();
int age = be.getAge();
System.out.println(name+"---"+age);
}
@Test
public void method2() throws Exception {
Class clazz = Class.forName("bull04.Method.Bean");
Object obj = clazz.newInstance();
Method methodSetName = clazz.getMethod("setName", String.class);
Method methodSetAge = clazz.getMethod("setAge", int.class);
methodSetName.invoke(obj, "aaa");
methodSetAge.invoke(obj, 10);
System.out.println(obj);
}
@Test
public void method3() throws Exception {
Class clazz = Class.forName("bull04.Method.Bean");
Object obj = clazz.newInstance();
Method methodSetName = clazz.getMethod("setName", String.class);
Method methodSetAge = clazz.getMethod("setAge", int.class);
methodSetName.invoke(obj, "aaa");
methodSetAge.invoke(obj, 10);
Method methodGetName = clazz.getMethod("getName");
Method methodGetAge = clazz.getMethod("getAge");
String str = (String) methodGetName.invoke(obj);
int age = (int) methodGetAge.invoke(obj);
System.out.println(str+"---"+age);
}
}
package bull04.Method;
import java.lang.reflect.Method;
import org.junit.Test;
public class PrivateMethod {
@Test
public void method () throws Exception {
Class clazz = Class.forName("bull04.Method.Bean");
Object obj = clazz.newInstance();
Method showMethod = clazz.getDeclaredMethod("show", int.class);
showMethod.setAccessible(true);
Object r = showMethod.invoke(obj, 18);
System.out.println(r);
}
}
package bull04.Method;
import java.lang.reflect.Method;
import org.junit.Test;
public class MainMethod {
@Test
public void method() throws Exception {
Class clazz = Class.forName("bull04.Method.Bean");
Method mainMethod = clazz.getMethod("main", String[].class);
String[] args = {"abc","123","xxx"};
mainMethod.invoke(null, (Object)args);
mainMethod.invoke(null, new Object[] {args});
}
}