JAVA反射机制是在
运行状态
中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。
Eg1:通过一个对象获得完整的包名和类名
package Reflect;
/**
* 通过一个对象获得完整的包名和类名
* */
class Demo{
//other codes...
}
class hello{
public static void main(String[] args) {
Demo demo=new Demo();
System.out.println(demo.getClass().getName());
}
}
【运行结果】:Reflect.Demo
Eg2:实例化Class对象
package Reflect;
class Demo{
//other codes...
}
class hello{
public static void main(String[] args) {
Class<?> demo1=null;
Class<?> demo2=null;
Class<?> demo3=null;
try {
//一般尽量采用这种形式
demo1 = Class.forName("Reflect.Demo");
}catch(Exception e){
e.printStackTrace();
}
demo2 = new Demo().getClass();
demo3=Demo.class;
}
}
Eg3:通过Class实例化其他类的对象
package Reflect;
class Person{
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;
}
@Override
public String toString(){
return "["+this.name+" "+this.age+"]";
}
private String name;
private int age;
}
class hello{
public static void main(String[] args) {
Class<?> demo=null;
try {
demo = Class.forName("Reflect.Person");
} catch (Exception e) {
e.printStackTrace();
}
try {
Person person = (Person) demo.newInstance();//实例化对象
person.setAge(20);
person.setName("test");
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
但是注意一下,当我们把Person中的默认的无参构造函数取消的时候,比如自己定义只定义一个有参数的构造函数之后,会出现错误,
所以在编写使用Class实例化其他类的对象的时候,一定要自己定义无参的构造函数。
Eg4:过Class调用其他类中的构造函数 (也可以通过这种方式通过Class创建其他类的对象)
package Reflect;
import java.lang.reflect.Constructor;
class Person{
public Person() {
}
public Person(String name){
this.name=name;
}
public Person(int age){
this.age=age;
}
public Person(String name, int age) {
this.age=age;
this.name=name;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString(){
return "["+this.name+" "+this.age+"]";
}
private String name;
private int age;
}
class hello{
public static void main(String[] args) {
Class<?> demo=null;
try{
demo=Class.forName("Reflect.Person");
}catch (Exception e) {
e.printStackTrace();
}
Person per1=null;
Person per2=null;
Person per3=null;
Person per4=null;
//取得全部的构造函数
Constructor<?> cons[]=demo.getConstructors();
try{
per1=(Person)cons[0].newInstance();
per2=(Person)cons[1].newInstance("Rollen");
per3=(Person)cons[2].newInstance(20);
per4=(Person)cons[3].newInstance("Rollen",20);
}catch(Exception e){
e.printStackTrace();
}
}
}
Eg5:返回一个类实现的接口
package Reflect;
interface China{
public static final String name="Rollen";
public static int age=20;
public void sayChina();
public void sayHello(String name, int age);
}
class Person implements China{
public Person() {
}
public Person(String sex){
this.sex=sex;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public void sayChina(){
System.out.println("hello ,china");
}
@Override
public void sayHello(String name, int age){
System.out.println(name+" "+age);
}
private String sex;
}
class hello{
public static void main(String[] args) {
Class<?> demo=null;
try{
demo=Class.forName("Reflect.Person");
}catch (Exception e) {
e.printStackTrace();
}
//保存所有的接口
Class<?> intes[]=demo.getInterfaces();
for (int i = 0; i < intes.length; i++) {
System.out.println("实现的接口 "+intes[i].getName());
}
}
}
Eg6
:取得其他类中的父类
class hello{
public static void main(String[] args) {
Class<?> demo=null;
try{
demo=Class.forName("Reflect.Person");
}catch (Exception e) {
e.printStackTrace();
}
//取得父类
Class<?> temp=demo.getSuperclass();
System.out.println("继承的父类为: "+temp.getName());
}
}
构造方法: public Reflect.Person()
构造方法: public Reflect.Person(java.lang.String)
public class Hello {
public static void main(String[] args) {
Class<?> demo = null;
try {
demo = Class.forName("reflect.Person");
} catch (Exception e) {
e.printStackTrace();
}
//获取全部的构造函数
Constructor<?>[] constructor = demo.getConstructors();
for (int i = 0; i < constructor.length; i++) {
//按照声明顺序返回一组 Class 对象,这些对象表示此 Constructor 对象所表示构造方法的形参类型。
Class<?>[] pClass = constructor[i].getParameterTypes();
System.out.print("构造方法: ");
//以整数形式返回此 Constructor 对象所表示构造方法的 Java 语言修饰符。
int mo=constructor[i].getModifiers();
System.out.print(Modifier.toString(mo)+" ");
System.out.print(constructor[i].getName());
System.out.print("(");
for (int j = 0; j < pClass.length; j++) {
System.out.print(pClass[j].getName()+" arg"+i);
if(j<pClass.length-1){
System.out.print(",");
}
}
System.out.println("){}");
}
}
}
构造方法: public reflect.Person(){}
构造方法: public reflect.Person(java.lang.String arg1){}
Eg7:接下来让我们取得其他类的全部属性吧,最后我讲这些整理在一起,也就是通过class取得一个类的全部框架
public class Hello {
public static void main(String[] args) {
Class<?> demo = null;
try {
demo = Class.forName("reflect.Person");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("===============本类属性========================");
// 取得本类的全部属性
//这些对象反映此 Class 对象所表示的类或接口所声明的所有字段。
Field[] field = demo.getDeclaredFields();
for (int i = 0; i < field.length; i++) {
// 权限修饰符
int mo = field[i].getModifiers();
String priv = Modifier.toString(mo);
// 属性类型
Class<?> type = field[i].getType();
System.out.println(priv + " " + type.getName() + " "+ field[i].getName() + ";");
}
System.out.println("===============实现的接口或者父类的属性========================");
// 取得实现的接口或者父类的属性
//这些对象反映此 Class 对象所表示的类或接口的所有可访问公共字段。
Field[] filed1 = demo.getFields();
for (int j = 0; j < filed1.length; j++) {
// 权限修饰符
int mo = filed1[j].getModifiers();
String priv = Modifier.toString(mo);
// 属性类型
Class<?> type = filed1[j].getType();
System.out.println(priv + " " + type.getName() + " "
+ filed1[j].getName() + ";");
}
}
}