共计 5409 个字符,预计需要花费 14 分钟才能阅读完成。
Java 反射机制,是在程序的运行过程中,对于任何一个类,都可能晓得它的所有属性和办法。对于任意一个对象,都可能晓得调用他的任意属性和办法。这种动静获取信息以及动静调用对象办法的性能称为 Java 语言的反射机制。
如下代码示例:
- 父类 Animal
package com.study.reflection;
public class Animal {
public String name;
public int age;
public Animal() {super();
}
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void eat() {}
public String getInfo() {return "name:" + this.name + ", age:" + age;}
}
- 子类 Cat,继承 Animal
package com.study.reflection;
public class Cat extends Animal {
public String color;
private String owner;
String sex;
public Cat() {super();
}
public Cat(String name, int age, String color, String owner) {super(name, age);
this.color = color;
this.owner = owner;
}
public Cat(String owner) {this.owner = owner;}
public String getColor() {return color;}
public void setColor(String color) {this.color = color;}
void mie() {}
private void smile() {System.out.println(name + "在笑");
}
public void cry() {System.out.println(name + "在哭");
}
@Override
public String toString() {return "姓名:" + this.name + ", 年龄:" + this.age + ", 色彩:" + color + ", 客人:" + owner;}
}
- 测试类 TestReflect
package com.study.reflection;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class TestReflect {public static void main(String[] args) {
Class cat = null;
try {cat = Class.forName("com.study.reflection.Cat");
} catch (ClassNotFoundException e) {e.printStackTrace();
}
// 获取对象的所有私有(public)属性,例如:public String color; 包含继承的父类中 public 润饰的属性
Field[] fields = cat.getFields();
for (Field f : fields) {
// 输入后果:// public java.lang.String com.study.reflection.Cat.color
// public java.lang.String com.study.reflection.Animal.name
// public int com.study.reflection.Animal.age
System.out.println(f);
}
System.out.println("==========================================================");
// 获取对象所有属性(不论是 public 还是 private 或者没有修饰符的润饰的属性),但不包含继承的父类中的属性
Field[] declaredFields = cat.getDeclaredFields();
for (Field df : declaredFields) {
// 输入后果://public java.lang.String com.study.reflection.Cat.color
//private java.lang.String com.study.reflection.Cat.owner
//java.lang.String com.study.reflection.Cat.sex
System.out.println(df);
}
System.out.println("==========================================================");
// 获取对象的所有公共(public)办法; 包含继承的父类中的 public 润饰的办法以及 Object 类中 public 润饰的办法
Method[] methods = cat.getMethods();
for (Method m : methods) {
// 输入后果://public java.lang.String com.study.reflection.Cat.toString()
//public void com.study.reflection.Cat.setColor(java.lang.String)
//public java.lang.String com.study.reflection.Cat.getColor()
//public void com.study.reflection.Cat.cry()
//public void com.study.reflection.Animal.eat()
//public java.lang.String com.study.reflection.Animal.getInfo()
//public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
//public final void java.lang.Object.wait() throws java.lang.InterruptedException
//public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
//public boolean java.lang.Object.equals(java.lang.Object)
//public native int java.lang.Object.hashCode()
//public final native java.lang.Class java.lang.Object.getClass()
//public final native void java.lang.Object.notify()
//public final native void java.lang.Object.notifyAll()
System.out.println(m);
}
System.out.println("==========================================================");
// 获取对象所有办法(不论是 public 还是 private 或者没有修饰符的润饰的办法),但不包含继承的父类中的办法
Method[] declaredMethods = cat.getDeclaredMethods();
for (Method dm : declaredMethods) {
// 输入后果://public java.lang.String com.study.reflection.Cat.toString()
//public void com.study.reflection.Cat.setColor(java.lang.String)
//public java.lang.String com.study.reflection.Cat.getColor()
//void com.study.reflection.Cat.mie()
//private void com.study.reflection.Cat.smile()
//public void com.study.reflection.Cat.cry()
System.out.println(dm);
}
System.out.println("==========================================================");
// 获取对象的所有公共(public)构造方法,但不包含继承的父类中的构造方法
Constructor[] constructors = cat.getConstructors();
for (Constructor c : constructors) {
// 输入后果://public com.study.reflection.Cat(java.lang.String)
//public com.study.reflection.Cat(java.lang.String,int,java.lang.String,java.lang.String)
//public com.study.reflection.Cat()
System.out.println(c);
}
System.out.println("==========================================================");
// 获取对象的所有构造方法,Constructor[] declaredConstructors = cat.getDeclaredConstructors();
for (Constructor dc : declaredConstructors) {
// 输入后果://public com.study.reflection.Cat(java.lang.String)
//public com.study.reflection.Cat(java.lang.String,int,java.lang.String,java.lang.String)
//public com.study.reflection.Cat()
System.out.println(dc);
}
System.out.println("==========================================================");
//Cat cat1 = (Cat) cat.newInstance();// 此办法在 Java9 之后显示已过期
Constructor<Cat> constructor = null;
try {
// 获取全参数构造函数
constructor = cat.getConstructor(String.class, int.class, String.class, String.class);
} catch (NoSuchMethodException e) {e.printStackTrace();
}
Cat cat1 = null;
try {
// 应用构造函数赋值初始化
cat1 = constructor.newInstance("花花", 2, "红色", "小明");
} catch (InstantiationException e) {e.printStackTrace();
} catch (IllegalAccessException e) {e.printStackTrace();
} catch (InvocationTargetException e) {e.printStackTrace();
}
// 输入后果:// 姓名:花花, 年龄:2, 色彩:红色, 客人:小明
System.out.println(cat1);
System.out.println("==========================================================");
// 获取指定办法,并执行,【获取的办法必须是 public 润饰的】Method cry = null;
try {
// 输入后果:// 花花在哭
cry = cat.getMethod("cry");//cry() 办法} catch (NoSuchMethodException e) {e.printStackTrace();
}
Object object = null;
try {object = cry.invoke(cat1);// 调用 cry() 办法} catch (IllegalAccessException e) {e.printStackTrace();
} catch (InvocationTargetException e) {e.printStackTrace();
}
}
}
正文完