列举java语言中反射的常用方法及实例代码
Java反射机制
一、什么是反射机制
简单的来说,反射机制指的是程序在运行时能够获取自身的信息。在java中,只要给定类的名字,
那么就可以通过反射机制来获得类的所有信息。
二、哪里用到反射机制
有些时候,我们用过一些知识,但是并不知道它的专业术语是什么,在刚刚学jdbc时用过一行代码,
Class.forName("com.mysql.jdbc.Driver.class").newInstance();但是那时候只知道那行代码是生成
驱动对象实例,并不知道它的具体含义。听了反射机制这节课后,才知道,原来这就是反射,现在很多开
框架都用到反射机制,hibernate、struts都是用反射机制实现的。
三、反射机制的优点与缺点
为什么要用反射机制?直接创建对象不就可以了吗,这就涉及到了动态与静态的概念,
静态编译:在编译时确定类型,绑定对象,即通过。
动态编译:运行时确定类型,绑定对象。动态编译最大限度发挥了java的灵活性,体现了多
态的应用,有以降低类之间的藕合性。
一句话,反射机制的优点就是可以实现动态创建对象和编译,体现出很大的灵活性,特别是在J2EE的开发中它的灵活性就表现的十分明显。比如,一个大型的软件,不可能一次就把把它设计的很完美,当这个程序编译后,发布了,当发现需要更新某些功能时,我们不可能要用户把以前的卸载,再重新安装新的版本,假如这样的话,这个软件肯定是没有多少人用的。采用静态的话,需要把整个程序重新编译一次才可以实现功能的更新,而采用反射机制的话,它就可以不用卸载,只需要在运行时才动态的创建和编译,就可以实现该功能。
它的缺点是对性能有影响。使用反射基本上是一种解释操作,我们可以告诉JVM,我们希望做什么并且它
满足我们的要求。这类操作总是慢于只直接执行相同的操作。
列举java语言中反射的常用方法
packagereview;/*12:432019/7/21*/
importmodel.AnotherClass;
importmodel.OneClassMore;
importmodel.SomeClass;
importjava.lang.reflect.Constructor;
importjava.lang.reflect.Field;
importjava.lang.reflect.InvocationTargetException;
importjava.lang.reflect.Method;
/**
*这个类列举了java语言中关于反射机制的常用的一些方法
*@authorzhangxingshuo
*/
publicclassAboutReflection{
publicstaticvoidmain(String[]args)throwsException{
}
/*获得Class对象的3种方式*/
privatestaticClass>getClazz0(StringclassName)throwsClassNotFoundException{
Classclazz=Class.forName(className);
returnclazz;
}
privatestaticClass>getClazz1(Objectobject){
Classclazz=object.getClass();
returnclazz;
}
privatestaticClass>getClazz2(){
Classclazz=model.SomeClass.class;
returnclazz;
}
/*经常使用的Class对象的3个方法*/
privatestaticStringuseClazz0(Classclazz){
StringfullyQualifiedName=clazz.getName();
returnfullyQualifiedName;
}
privatestaticStringuseClazz1(Classclazz){
StringclassName=clazz.getSimpleName();
returnclassName;
}//ex:private//ex:abstract
privatestaticObjectuseClazz2(Classclazz)throwsIllegalAccessException,InstantiationException{
Objectobject=clazz.newInstance();
returnobject;
}
/*获得Constructor对象的3个方法*/
privatestaticConstructor>[]getConstructorObject0(Classclazz){
Constructor>[]constructors=clazz.getConstructors();
returnconstructors;
}
privatestaticConstructor>[]getConstructorObject1(Classclazz){
Constructor>[]constructors=clazz.getDeclaredConstructors();
returnconstructors;
}
privatestaticConstructor>getConstructorObject2(Classclazz)throwsNoSuchMethodException{
Constructor>constructor=clazz.getConstructor(SomeClass.class,AnotherClass.class,OneClassMore.class);
returnconstructor;
}
privatestaticConstructor>getConstructorObject3(Classclazz)throwsNoSuchMethodException{
Constructor>constructor=clazz.getDeclaredConstructor(SomeClass.class,AnotherClass.class,OneClassMore.class);
returnconstructor;
}
/*经常使用的Constructor对象的2个方法*/
privatestaticObjectuseConstructorObject0(Constructor>constructor)throwsIllegalAccessException,InvocationTargetException,InstantiationException{
//underhere,ifthevariableoverride==true,jvmwilllnotchecktheaccessiblemodifier
Objectobject=constructor.newInstance(newSomeClass(),newAnotherClass(),newOneClassMore());
returnobject;
}
privatestaticvoiduseConstructorObject1(Constructor>constructor){
//underherechanging"override"variable'svaluewhoisdefinedinAccessibleObject,the"superandsuper"ClassofConstructor
constructor.setAccessible(true);
}
/*还有一些*/
privatestaticClass>useConstructorObject2(Constructor>constructor){
Classclazz=constructor.getDeclaringClass();
returnclazz;
}
privatestaticintuseConstructorObject3(Constructor>constructor){
intmodifiers=constructor.getModifiers();
returnmodifiers;
}
privatestaticStringuseConstructorObject4(Constructor>constructor){
//constructornameissameastheclassname
StringconstructorName=constructor.getName();
//underheregetDeclaringClass().getName();
returnconstructorName;
}
/*获取Field对象的4个方法*/
privatestaticField[]getFieldObject0(Classclazz){
Field[]fields=clazz.getFields();
returnfields;
}
privatestaticField[]getFieldObject1(Classclazz){
Field[]declaredFields=clazz.getDeclaredFields();
returndeclaredFields;
}
privatestaticFieldgetFieldObject2(Classclazz)throwsNoSuchFieldException{
Fieldfield=clazz.getField("theFieldName");
returnfield;
}
privatestaticFieldgetField3(Classclazz)throwsNoSuchFieldException{
Fieldfield=clazz.getDeclaredField("theFieldName");
returnfield;
}
/*经常使用的Field对象的3个方法*/
privatestaticObjectuseFieldObject0(Fieldfield,Objectobject)throwsIllegalAccessException{
ObjectfieldValue=field.get(object);
returnfieldValue;
}
privatestaticvoiduseFieldObject1(Fieldfield,Objectobject)throwsIllegalAccessException{
//anobjectasthefieldvalue
field.set(object,newObject());
}
privatestaticvoiduseFieldObject2(Fieldfield){
//sameprocess
field.setAccessible(true);
}
/*还有一些*/
privatestaticintuseFieldObject3(Fieldfield){
intmodifiers=field.getModifiers();
returnmodifiers;
}
privatestaticStringuseFieldObject4(Fieldfield){
StringfieldName=field.getName();
returnfieldName;
}
/*获取Method对象的4个方法*/
privatestaticMethod[]getMethodObject0(Classclazz){
Method[]methods=clazz.getMethods();
returnmethods;
}
privatestaticMethod[]getMethodObject1(Classclazz){
Method[]methods=clazz.getDeclaredMethods();
returnmethods;
}
privatestaticMethodgetMethodObject2(Classclazz)throwsNoSuchMethodException{
Methodmethod=clazz.getMethod("someMethodName",SomeClass.class,AnotherClass.class,OneClassMore.class);
returnmethod;
}
privatestaticMethodgetMethodObject3(Classclazz)throwsNoSuchMethodException{
Methodmethod=clazz.getDeclaredMethod("someMethodName",SomeClass.class,AnotherClass.class,OneClassMore.class);
returnmethod;
}
/*经常使用的Field对象的2个方法*/
privatestaticObjectuseMethodObject0(Methodmethod,Objectobject)throwsInvocationTargetException,IllegalAccessException{
Objectreturnedobject=method.invoke(object,newSomeClass(),newAnotherClass(),newOneClassMore());
returnreturnedobject;
}
privatestaticvoiduseMethodObject1(Methodmethod){
method.setAccessible(true);
}
/*还有一些*/
privatestaticintuseMethodObject2(Methodmethod){
intmodifiers=method.getModifiers();
returnmodifiers;
}
privatestaticStringuseMethodObject3(Methodmethod){
StringmethodName=method.getName();
returnmethodName;
}
/*
tips
通过getMethods(),得到该类或接口独有的和继承自它的所有父类与接口的public方法组成的数组.
通过getDeclaredMethods(),得到该类或接口独有的所有方法,(包括public和非public).
*/
/*justasaemptytemplateforconvenience*/
privatestaticvoidm(){
}
}
总结
以上所述是小编给大家介绍的列举java语言中反射的常用方法及实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!