public class Person {
public String name; //字段或成员变量
private int age;
public final String password = "123"; //字段或成员变量
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;
}
public Person(){
//System.out.println("person!!!!!");
}
public Person(String name){
System.out.println(name);
}
private Person(int name){
System.out.println(name);
}
public void eat(){
System.out.println("eat!!!!");
}
public void run(String address){
System.out.println("跑到" + address);
}
public void run(String address,int num[],String ss[]){
System.out.println("跑到" + address + "," + num);
}
public String test(String str){
return str + "aaaa";
}
private String test2(String str){
return str + "aaaa";
}
public static String test3(String str){
return str + "aaaa";
}
public static void main(String[] args) {
System.out.println(args[0]);
}
}
//第一种
Class clazz1 = Class.forName("cn.csx.reflect.Person");
//第二种
Class clazz2 = Person.class;
//第三种
Class clazz3 = new Person().getClass();
//创建对象
Person p = (Person) clazz1.newInstance();
反射类无参的构造方法
//反射类无参的构造方法
@Test
public void test1() throws ClassNotFoundException,
NoSuchMethodException, InstantiationException,
IllegalAccessException, InvocationTargetException {
Class clazz = Class.forName("cn.csx.reflect.Person");
Constructor c = clazz.getConstructor(null);
Object obj = c.newInstance(null);
System.out.println(obj);
}
反射类有参的构造方法:public Person(String name)
//反射类有参的构造方法:public Person(String name)
@Test
public void test2() throws ClassNotFoundException,
NoSuchMethodException, InstantiationException,
IllegalAccessException, InvocationTargetException {
Class clazz = Class.forName("cn.csx.reflect.Person");
Constructor c = clazz.getConstructor(String.class);
Person p = (Person) c.newInstance("flx");
System.out.println(p);
}
反射类私有的、有参的构造方法:private Person(int name)(暴力反射)
//反射类私有的、有参的构造方法:private Person(int name)
@Test
public void test3() throws ClassNotFoundException,
NoSuchMethodException, InstantiationException,
IllegalAccessException, InvocationTargetException {
Class clazz = Class.forName("cn.csx.reflect.Person");
Constructor c = clazz.getDeclaredConstructor(int.class);
c.setAccessible(true);//暴力反射
Person p = (Person) c.newInstance(1);
System.out.println(p);
}
反射方法
public class Demo3 {
//反射方法
//反射:public void eat()
@Test
public void test1() throws Exception{
Person p = new Person(); //
Class clazz = Class.forName("cn.csx.reflect.Person"); //完整名称
Method method = clazz.getMethod("eat", null); //eat
method.invoke(p, null); //eat
}
//反射:run(String address){
@Test
public void test2() throws Exception{
Person p = new Person(); //
Class clazz = Class.forName("cn.csx.reflect.Person"); //完整名称
Method method = clazz.getMethod("run", String.class);
method.invoke(p, "北京");
}
//反射:public void run(String address,int num[],String ss[]){
@Test
public void test3() throws Exception{
Person p = new Person(); //
Class clazz = Class.forName("cn.csx.reflect.Person"); //完整名称
Method method = clazz.getMethod("run", String.class,int[].class,String[].class);
method.invoke(p, "上海",new int[]{1,2},new String[]{"1","2"});
}
//反射:public String test(String str) (带返回值)
@Test
public void test4() throws Exception{
Person p = new Person(); //
Class clazz = Class.forName("cn.csx.reflect.Person"); //完整名称
Method method = clazz.getMethod("test", String.class);
String result = (String) method.invoke(p, "xxxx");
System.out.println(result);
}
//反射:private String test2(String str) 私有方法
@Test
public void test5() throws Exception{
Person p = new Person(); //
Class clazz = Class.forName("cn.csx.reflect.Person"); //完整名称
Method method = clazz.getDeclaredMethod("test2", String.class);
method.setAccessible(true);
method.invoke(p, "");
}
//反射:public static String test3(String str){ 静态方法
@Test
public void test6() throws Exception{
Class clazz = Class.forName("cn.csx.reflect.Person"); //完整名称
Method method = clazz.getMethod("test3", String.class);
String result = (String) method.invoke(null, "aaa");
System.out.println(result);
}
//反射:public static void main(String[] args) { 反射main方法
//通过反射调用带数组的方法,要注意处理
@Test
public void test7() throws Exception{
Class clazz = Class.forName("cn.csx.reflect.Person"); //完整名称
Method method = clazz.getMethod("main", String[].class);
method.invoke(null, (Object)new String[]{"1","2"});//main(String args[])
//method.invoke(null, new Object[]{new String[]{"1","2"}});//main(String args[])
//public Object invoke(Object obj, Object... args) //jdk1.5
//public Object invoke(Object obj, Object[] args) //jdk1.4
//public void run(String name,String password)
//method.invoke(p,new Object[]{"flx,123"})//1.4
//method.invoke(p,"flx","123");
}
}
反射类的字段
public class Demo4 {
/**反射类的字段
* @param args
* @throws Exception
*/
//反射:public String name;
@Test
public void test1() throws Exception{
Person p = new Person();
Class clazz = Class.forName("cn.csx.reflect.Person");
Field f = clazz.getField("name"); //name
f.set(p, "flx");
System.out.println(p.getName());
}
//反射:public String name;
@Test
public void test2() throws Exception{
Person p = new Person();
p.setName("xxx");
Class clazz = Class.forName("cn.csx.reflect.Person");
Field f = clazz.getField("name"); //name
String result = (String) f.get(p);
System.out.println(result);
}
//反射:public final String password = ""; //字段或成员变量
@Test
public void test3() throws Exception{
Person p = new Person();
Class clazz = Class.forName("cn.csx.reflect.Person");
Field f = clazz.getField("password"); //name
String result = (String)f.get(p);
System.out.println(result);
}
//反射:private int age; 私有字段
@Test
public void test4() throws Exception{
Person p = new Person();
Class clazz = Class.forName("cn.csx.reflect.Person");
Field f = clazz.getDeclaredField("age");
f.setAccessible(true);
f.set(p, 123);
int result = (Integer) f.get(p);
System.out.println(result);
}
}