标签:one author 创建 field instant win mem lang cep
- JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制。
- 配合这位多情剑客无情剑;老哥的这篇博客,讲的非常透彻。
public class GetConstructor {
public static void main(String[] args) throws ClassNotFoundException,
NoSuchMethodException, SecurityException, InstantiationException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class class1 = Class.forName("com.oracle.test01.Person");
// 获取公共Constructor列表
Constructor[] cons = class1.getConstructors();
// 遍历列表
for (Constructor s : cons) {
System.out.println(s);
}
// *************************************************
// 获取全部Constructor列表
Constructor[] consd = class1.getDeclaredConstructors();
// 遍历列表
for (Constructor s : consd) {
System.out.println(s);
}
// *************************************************
// 获取单个Constructor
Constructor con = class1.getConstructor(String.class);
// 利用这构造方法实例化
Person obj = (Person)con.newInstance("赵大宝");
System.out.println(obj.name);
// TODO null
// 获取包括限制的Constructor
Constructor cond = class1.getDeclaredConstructor(String.class, int.class);
cond.setAccessible(true);
Person p = (Person)cond.newInstance("赵二宝", 1);
System.out.println(p.toString());
// TODO Person [name=null, age=0]
}
}
- invok()
package com.oracle.test01;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* 获取成员方法
* @author cool101
*
*/
public class GetMethod {
public static void main(String[] args) throws NoSuchMethodException, SecurityException,
InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// 获取Class对象
Class c = Person.class;
// 获取方法
Method m = c.getMethod("eat");
// new对象
Person p = (Person)c.newInstance();
// 执行方法
m.invoke(p);
/*Class c = Person.class;
Method m = c.getMethod("eat");
Person p = (Person)c.newInstance();
m.invoke(p);*/
}
}
public class GetMember {
public static void main(String[] args) throws NoSuchFieldException, SecurityException,
InstantiationException, IllegalAccessException, ClassNotFoundException {
// 获取Class对象
Class class1 = Class.forName("com.oracle.test01.Person");
// 创建实例
Person p = (Person)class1.newInstance();
// 获取name成员变量
Field f = class1.getField("name");
// 赋值
f.set(p, "韦大宝");
// 打印
System.out.println(p.toString());
}
}
Person
public class Person {
public String name;
private int age;
public Person() {
System.out.println("公共空参");
}
public Person(String name){
System.out.println("public Person(String name)");
}
@SuppressWarnings("unused")
private Person(String name, int age){
System.out.println("private Perosn(String name, int age)");
}
static {
System.out.println("静态代码块");
}
public void eat() {
System.out.println("public void eat()");
}
public void sleep(){
System.out.println("public void sleep()");
}
public void smoke(){
System.out.println("public void smoke()");
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
标签:one author 创建 field instant win mem lang cep
原文地址:https://www.cnblogs.com/torain/p/12797651.html