package cn.itcast;
public class Person {
public String name="aaa";
private String password="hhh";
public static String repassword="dadwad";
public Person()
{
System.out.println("person");
}
public Person(String name)
{
this.name=name;
System.out.println(this.name);
}
public Person(String name,int[] score)
{
this.name=name;
System.out.println(this.name);
for(Object obj:score)
{
System.out.println(obj);
}
}
private Person(String password,int id)
{
this.password=password;
System.out.println(this.password+" "+id);
}
public void add()
{
System.out.println("person.add()");
}
public void add(String name)
{
this.name=name;
System.out.println(name);
}
private int add(int a,int b)
{
return a+b;
}
public static void mian(String[] arg0) {
// TODO Auto-generated method stub
System.out.print("main()");
}
}
package cn.itcast;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//test1();
//test2();
//text3();
//text4();
//text5();
//text6();
//text7();
//text8();
//text9();
text10();
}
//反射私有字段
private static void text10() throws ClassNotFoundException,
NoSuchFieldException, IllegalAccessException {
Person p=new Person();
Class clazz=Class.forName("cn.itcast.Person");
Field name = clazz.getField("name");
name.setAccessible(true);
System.out.println(name.get(p));
name.set(p,"hhah");
System.out.println(name.get(p));
}
//反射公有字段
private static void text9() throws ClassNotFoundException,
NoSuchFieldException, IllegalAccessException {
Person p=new Person();
Class clazz=Class.forName("cn.itcast.Person");
Field name = clazz.getField("name");
System.out.print(name.get(p));
}
//反射主函数
private static void text8() throws ClassNotFoundException,
NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
Person p=new Person();
Class clazz=Class.forName("cn.itcast.Person");
Method men = clazz.getDeclaredMethod("mian",String[].class);
men.setAccessible(true);
System.out.print(men.invoke(p,(Object)(new String[]{"123","awdw"})));
}
//反射私有有参函数
private static void text7() throws ClassNotFoundException,
NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
Person p=new Person();
Class clazz=Class.forName("cn.itcast.Person");
Method men = clazz.getDeclaredMethod("add",int.class,int.class);
men.setAccessible(true);
System.out.print(men.invoke(p, 1,2));
}
//反射有参函数
private static void text6() throws ClassNotFoundException,
NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
Person p=new Person();
Class clazz=Class.forName("cn.itcast.Person");
Method men = clazz.getMethod("add",String.class);
men.invoke(p,"zhangsan");
}
//反射无参函数
private static void text5() throws ClassNotFoundException,
NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
Person p=new Person();
Class clazz=Class.forName("cn.itcast.Person");
Method men = clazz.getMethod("add", null);
men.invoke(p, null);
}
//反射私有多参构造函数
private static void text4() throws ClassNotFoundException,
NoSuchMethodException, InstantiationException,
IllegalAccessException, InvocationTargetException {
Class clazz=Class.forName("cn.itcast.Person");
Constructor con = clazz.getDeclaredConstructor(String.class,int.class);
con.setAccessible(true);//强暴私有成员
Person p=(Person) con.newInstance("enen",12);
}
//反射多参构造方法
private static void text3() throws ClassNotFoundException,
NoSuchMethodException, InstantiationException,
IllegalAccessException, InvocationTargetException {
Class clazz=Class.forName("cn.itcast.Person");
Constructor con = clazz.getConstructor(String.class,int[].class);
Person p=(Person) con.newInstance("dawdwad",new int[]{1,2,3,5});
}
//反射有参构造方法
private static void test2() throws ClassNotFoundException,
NoSuchMethodException, InstantiationException,
IllegalAccessException, InvocationTargetException {
Class clazz=Class.forName("cn.itcast.Person");
Constructor con = clazz.getConstructor(String.class);
Person p=(Person) con.newInstance("zhangsan");
}
//反射无参构造方法
private static void test1() throws ClassNotFoundException,
NoSuchMethodException, InstantiationException,
IllegalAccessException, InvocationTargetException {
Class clazz=Class.forName("cn.itcast.Person");
Constructor can = clazz.getConstructor(null);
Person p=(Person) can.newInstance(null);
}
}