反射学习03

来源:互联网 发布:矩阵组织结构的优点是 编辑:程序博客网 时间:2024/04/29 00:38
package 反射;


import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;


class Persons {
private String name;
private int age;


public Persons() {
super();
// TODO 自动生成的构造函数存根
}


public Persons(String name) {
super();
this.name = name;
}

public Persons(String name, int age) {
super();
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;
}


public void show() {
System.out.println("公共方法1");
}


public void show1(String string) {
System.out.println("公共方法2" + string);
}


private void show2() {
System.out.println("私有方法1");
}


private void show3() {
System.out.println("私有方法2");
}


@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}


public class ReflectDemo08 {


public static void main(String[] args) throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
NoSuchMethodException, SecurityException, IllegalArgumentException,
InvocationTargetException, NoSuchFieldException {
/*
* 创建字节码对象的三种方式。
*/
// 1:利用每个类型的class属性
Class class1 = Persons.class;
// 2,利用Object对象的getClass()对象
Persons person = new Persons();


Class class2 = person.getClass();
// 3 非常提倡的方法。利用Class类的静态方法。。
Class class3 = Class.forName("反射.Persons");
System.out.println(class1 == class2);
System.out.println(class2 == class3);


/*
* 利用反射创建对象
*/


// 这个创建对象的过程是:首先根据new的名称找到字节码文件,然后创建字节码文件对象,然后创建字节码对应的Persons对象。
Persons p = new Persons();
p.setName("zbx");
p.setAge(27);
System.out.println(p.toString());
// //////////////////////////////
Persons pp = new Persons("zzbbxx", 27);
System.out.println(pp.toString());


/*
* 利用反射创建对象
*/
// 这个创建对象的过程是:首先根据名称找到字节码文件,然后创建字节码文件对象,然后创建构造方法没有任何参数的对象。
Class class4 = Class.forName("反射.Persons");
Persons p1 = (Persons) class4.newInstance();
System.out.println(p1.toString());
// 创建构造函数有参数的对象
Class class5 = Class.forName("反射.Persons");
Constructor c = class5.getConstructor(String.class, int.class);
Persons pp1 = (Persons) c.newInstance("zzzbbbxxx", 27);
System.out.println(pp1.toString());
/*
* 通过反射调用变量
*/
Class class6 = Class.forName("反射.Persons");
Field name = class6.getDeclaredField("name");
Field age = class6.getDeclaredField("age");
// 对私有对象取消私有化
System.out.println(name);
System.out.println(age);
System.out.println("------------------------------------------");
Persons p3 = (Persons) class6.newInstance();
p3.setAge(27);
p3.setName("zzzzbbbbxxx");
// 对私有对象取消私有化
name.setAccessible(true);
String str_name = (String) name.get(p3);
System.out.println(str_name);
// 同样的age的方法方式和name的方法方式完全一样。。
/*
* 通过反射调用方法
*/
Class class7 = Class.forName("反射.Persons");
// Method []methods=class7.getMethods();
Method[] methods = class7.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method);
}


// 输出具体的对象的内容
// 1,调用的是没有参数的函数
Class class8 = Class.forName("反射.Persons");
Method method = class8.getMethod("show", null);
Persons p4 = (Persons) class8.newInstance();
p4.setAge(27);
p4.setName("zzzzzbbbbbxxxxx");
method.invoke(p4, null);


// 2,调用的是有参数的函数
Class class9 = Class.forName("反射.Persons");
Method method1 = class9.getMethod("show1", String.class);
Persons p5 = (Persons) class9.newInstance();
method1.invoke(p5,"aaa");
}


}
0 0
原创粉丝点击