JAVA-反射-构造器、方法、成员变量
2021-02-04 01:15
标签:one author 创建 field instant win mem lang cep Person JAVA-反射-构造器、方法、成员变量 标签:one author 创建 field instant win mem lang cep 原文地址:https://www.cnblogs.com/torain/p/12797651.htmlJAVA-反射-构造器、方法、成员变量
反射定义
反射获取构造方法
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]
}
}
反射获取方法
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());
}
}
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 + "]";
}
}
上一篇:python 之装饰器
下一篇:数组常用方法有哪些?
文章标题:JAVA-反射-构造器、方法、成员变量
文章链接:http://soscw.com/index.php/essay/50664.html