java反射:
官方理解:
反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问、检测和修改其本身状态或行为的一种能力。在Java环境中,反射机制允许程序在执行时获取某个类自身的定义信息,例如熟悉和方法等也可以实现动态创建类的对象、变更属性的内容或执行特定的方法的功能。从而使Java具有动态语言的特性,增强了程序的灵活性和可移植性
自己的理解:
为了正向运行而反向操作本身的操作.
Java反射机制API
实现Java反射机制的API在Java.lang.reflect包下
(1)Class类:类对象
(2)Filed类:类的成员变量。
(3)Method类:类的方法。
(4)Constructor类:类的构造方法。
( 5 ) Array类 : 提供了动态创建数组及访问数组元素的静态方法,该类中的所有方法都是静态的.
代码示例:
1.class类的获取
/**
*
- @author dch
*/
//获取class对象
public class ClassDemo {
public static void main(String[] args) throws ClassNotFoundException {
//1.通过Class.forName()获取
Class c1 = Class.forName("com.dch.test.Person");
//2.通过类名.class获得Class对象
Class c2 = Person.class;
//3.通过Class对象获取getClass();
Person p = new Person();
Class c3 = p.getClass();
//获取类名
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
}
//注:获取了class对象,就可以知道该类的属性和方法.
}
2.获取类的成员变量,方法.
定义了一个Person类:
/**
*
- @author dch
*/
//定义一个person类
public class Person {
//成员变量
//名字
private String name;
//年龄
private int age;
//身高
private float height;
//无参构造器
public Person() {
}
//有参构造器
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
//属性
//设置get/set方法
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 double getWeight(){
return 65.50;
}
public void setWeight(double weight){
}
@Override
public String toString() {
return "Person [姓名: " + name + ", 年龄: " + age + "]";
}
/* //测试方法
public static void main(String[] args) {
Person p1 = new Person("杜昌豪", 22);
Person p2 = new Person();
p2.setName("熊");
p2.setAge(21);
System.out.println(p1);
System.out.println(p2);
}*/
}
/**
*
- @author dch
*/
//有了实体类后,就要对类中的属性进行操作
public class ClassDemo1 {
public static void main(String[] args) throws Exception{
//通过类名.class获取类对象
//(要明白获取了对象并不可以直接操作person类的方法和属性)
//(因为没有实例化)
//Class c1 = Integer.class;
//获取method属性;(获得单个的方法名)
//Method method = c1.getMethod("setName", new Class[]{String.class});
//获取method方法(获取所有的方法名)
//Method[] method1 = c1.getDeclaredMethods();
//将对象实例化
//Object obj = c1.newInstance();
//获取所有的属性包括私有属性
//Field[] field1 = c1.getDeclaredFields();
//获取方法的名称
//String methodName = method.getName();
//System.out.println(methodName);
/*for (Method m1 : method1) {
String methodName1 = m1.getName();
System.out.println(methodName1);
}*/
/*for (Field f1 : field1) {
System.out.println(f1.getName()+","+f1.getType());
}*/
/*//获取所有属性
Field[] fs = c1.getDeclaredFields();
for (Field f : fs) {
String name = f.getName();
//获取修饰符
int i =f.getModifiers();
System.out.println("获取属性"+name+","+"获取修饰符"+i);
}*/
/*Object arr = Array.newInstance(c1, 10);
//在数组5索引的位置赋值
Array.set(arr, 5, 6);
//获取数组5索引位置的值
Integer s = (Integer)Array.get(arr, 5);
System.out.println(s);*/
}
//反射:自己理解是,在程序运行过程当中获取对象的属性和方法,
//这里的属性和方法说的是它们的名字例如 public static void add(){}
//而不是说去调用这些方法,只是想知道它本身的构造.
public static void test1(){
Class c = String.class;
Method[] methods = c.getMethods();
for(int i = 0;i<methods.length;i++){
System.out.println(methods[i].getName());
}
Field[] fields = c.getFields();
for(Field f : fields){
System.out.println(f.getType()+":"+f.getName());
}
}
}