网站实名认证在哪,动漫制作技术专业介绍,网络推广方案的概念,做网站怎么实现鼠标经过图像目录 反射
定义
主要用途
反射相关的类
Class类中【获得类相关方法】
Class类中【获得类中属性相关的方法】
Class类中【获得类中注解相关的方法】 Class类中【获得类中构造器相关的方法】
Class类中【获得类中方法相关的方法】
获得Class对象
代码示例1
代码示例…
目录 反射
定义
主要用途
反射相关的类
Class类中【获得类相关方法】
Class类中【获得类中属性相关的方法】
Class类中【获得类中注解相关的方法】 Class类中【获得类中构造器相关的方法】
Class类中【获得类中方法相关的方法】
获得Class对象
代码示例1
代码示例2
反射的优缺点 反射
定义 Java的反射reflection机制是在运行状态中对于任意一个类都能够知道这个类的所有属性和方法对于任意一个对象都能够调用它的任意方法和属性既然能拿到那么我们就可以修改部分类型信息这种动态获取信息以及动态调用对象方法的功能称为java语言的反射reflection机制。 主要用途 1.动态地创建类的实例在运行时根据类的全限定名创建对象。 2.检查类的结构获取类的成员变量、方法、构造器等信息。 3.调用方法在运行时动态地调用对象的方法。 4.访问和修改私有字段即使在类定义中字段是私有的也可以通过反射来访问和修改。 反射相关的类
类名用途Class类代表类的实体在运行的Java应用程序中表示类和接口Filed类代表类的成员变量/类的属性Method类代表类的方法Constructor类代表类的构造方法 Class类
Class类代表类的实体在运行的Java应用程序中表示类和接口 . Java文件被编译后生成了.class文件JVM此时就要去解读.class文件 ,被编译后的Java文件.class也被JVM解析为一个对象这个对象就是java.lang.Class这样当程序在运行时每个.class文件就最终变成了Class类对象的一个实例。我们通过Java的反射机制应用到这个实例就可以去获得甚至去添加改变这个类的属性和动作使得这个类成为一个动态的类。 Class类中【获得类相关方法】
方法用途getClassLoader()获得类的加载器getDeclaredClasses()返回一个数组数组中包含该类的所有类和和接口类的对象包括私有的forName(String className)根据类名返回类的对象newInstance()创建类的实例getName()获得类的完整路径名字 Class类中【获得类中属性相关的方法】
方法用途getField(String name)获得某个公有的属性对象getFields()获得所有公有的属性对象getDeclaredField(String name)获得某个属性对象getDeclaredFields()获得所有属性对象 Class类中【获得类中注解相关的方法】
方法用途getAnnotation(Class annotationClass)返回该类中与参数匹配的公有注解对象getAnnotations()返回该类中所有的公有注解对象getDeclaredAnnotaion(Class annotationClass)返回该类中与参数类型匹配的所有注解对象getDeclaredAnnotations()返回该类所有的注解对象 Class类中【获得类中构造器相关的方法】
方法用途getConstructor(Class?... parameterTypes)获得该类中与参数类型匹配的公有构造方法getConstructors()获得该类的所有公有构造方法getDeclaredConstructor(Class?... parameterTypes)获得该类中与参数类型匹配的构造方法getDeclaredConstructors()获得该类所有构造方法 Class类中【获得类中方法相关的方法】
方法用途getMethod(String name,Class?... parameterTypes)获得该类某个公有的方法geMethods()获得该类所有公有的方法getDeclaredMethod(String name,Class?... parameterTypes)获得该类某个方法getDeclaredMethds()获得该类所有方法 获得Class对象
反射的第一步是获取代表某个类的Class对象。可以通过多种方式获取Class对象最常见的是 1.使用Class.forName(String className)静态方法如果类名在类的路径中则通过该类的全限定名包括包名来获取Class对象。注意这种方式会抛出ClassNotFoundException。 2.使用.class语法在编译时就已经确定。 3.调用对象的getClass()方法在运行时确定对象的实际类型。 代码示例1
class Student{//私有属性nameprivate String name bit;//公有属性agepublic int age 18;//不带参数的构造方法public Student(){System.out.println(Student());}private Student(String name,int age) {this.name name;this.age age;System.out.println(Student(String,name));}private void eat(){System.out.println(i am eat);}public void sleep(){System.out.println(i am pig);}private void function(String str) {System.out.println(str);}Overridepublic String toString() {return Student{ name name \ , age age };}
}public class Test {//Class对象 只有一个public static void main(String[] args) {Class? c1;try {c1 Class.forName(reflectdemo.Student);} catch (ClassNotFoundException e) {throw new RuntimeException(e);}Class? c2;c2 Student.class;Student student new Student();Class? c3 student.getClass();System.out.println(c1.equals(c2));System.out.println(c1.equals(c3));System.out.println(c3.equals(c2));}
}代码示例2
class Student{//私有属性nameprivate String name bit;//公有属性agepublic int age 18;//不带参数的构造方法public Student(){System.out.println(Student());}private Student(String name,int age) {this.name name;this.age age;System.out.println(Student(String,name));}private void eat(){System.out.println(i am eat);}public void sleep(){System.out.println(i am pig);}private void function(String str) {System.out.println(str);}Overridepublic String toString() {return Student{ name name \ , age age };}
}public class ReflectDemo {//如何通过反射 实例化对象public static void reflectNewInstance() {Class? c1;try {c1 Class.forName(reflectdemo.Student);Student student (Student) c1.newInstance();System.out.println(student);} catch (ClassNotFoundException e) {throw new RuntimeException(e);} catch (InstantiationException e) {throw new RuntimeException(e);} catch (IllegalAccessException e) {throw new RuntimeException(e);}}// 反射私有的构造方法 屏蔽内容为获得公有的构造方法public static void reflectPrivateConstructor() {Class? c1;try {c1 Class.forName(reflectdemo.Student);ConstructorStudent con (Constructor)c1.getDeclaredConstructor(String.class,int.class);con.setAccessible(true);Student student con.newInstance(zhangsan,18);System.out.println(student);} catch (ClassNotFoundException e) {throw new RuntimeException(e);} catch (NoSuchMethodException e) {throw new RuntimeException(e);} catch (InvocationTargetException e) {throw new RuntimeException(e);} catch (InstantiationException e) {throw new RuntimeException(e);} catch (IllegalAccessException e) {throw new RuntimeException(e);}}// 反射私有属性public static void reflectPrivateField() {Class? c1;try {c1 Class.forName(reflectdemo.Student);Field field c1.getDeclaredField(name);field.setAccessible(true);Student student (Student) c1.newInstance();field.set(student,wangwu);System.out.println(student);} catch (ClassNotFoundException e) {throw new RuntimeException(e);} catch (NoSuchFieldException e) {throw new RuntimeException(e);} catch (InstantiationException e) {throw new RuntimeException(e);} catch (IllegalAccessException e) {throw new RuntimeException(e);}}// 反射私有方法public static void reflectPrivateMethod() {Class? c1;try {c1 Class.forName(reflectdemo.Student);Method method c1.getDeclaredMethod(function,String.class);method.setAccessible(true);Student student (Student) c1.newInstance();method.invoke(student,我是一个参数);//System.out.println(student);} catch (ClassNotFoundException e) {throw new RuntimeException(e);} catch (NoSuchMethodException e) {throw new RuntimeException(e);} catch (InstantiationException e) {throw new RuntimeException(e);} catch (IllegalAccessException e) {throw new RuntimeException(e);} catch (InvocationTargetException e) {throw new RuntimeException(e);}}public static void main(String[] args) {//reflectNewInstance();//reflectPrivateConstructor();//reflectPrivateField();reflectPrivateMethod();}
}
反射的优缺点
优点 1. 对于任意一个类都能够知道这个类的所有属性和方法 对于任意一个对象都能够调用它的任意一个方法。 2. 增加程序的灵活性和扩展性降低耦合性提高自适应能力。 3. 反射已经运用在了很多流行框架如Struts、Hibernate、Spring 等等。 缺点 1.反射会破坏封装性使代码难以理解和维护。 2.反射通常比直接代码调用慢因为它涉及到类型检查等动态解析。 3.滥用反射可能导致安全问题如访问或修改不应该被访问的私有成员。
文章转载自: http://www.morning.kxymr.cn.gov.cn.kxymr.cn http://www.morning.xgbq.cn.gov.cn.xgbq.cn http://www.morning.ywqw.cn.gov.cn.ywqw.cn http://www.morning.nynpf.cn.gov.cn.nynpf.cn http://www.morning.cjqcx.cn.gov.cn.cjqcx.cn http://www.morning.hcwlq.cn.gov.cn.hcwlq.cn http://www.morning.nlqgb.cn.gov.cn.nlqgb.cn http://www.morning.dnjwm.cn.gov.cn.dnjwm.cn http://www.morning.rknhd.cn.gov.cn.rknhd.cn http://www.morning.ywqw.cn.gov.cn.ywqw.cn http://www.morning.fpjxs.cn.gov.cn.fpjxs.cn http://www.morning.jrhcp.cn.gov.cn.jrhcp.cn http://www.morning.ykswq.cn.gov.cn.ykswq.cn http://www.morning.cokcb.cn.gov.cn.cokcb.cn http://www.morning.jhrkm.cn.gov.cn.jhrkm.cn http://www.morning.trfh.cn.gov.cn.trfh.cn http://www.morning.bfrff.cn.gov.cn.bfrff.cn http://www.morning.hwnnm.cn.gov.cn.hwnnm.cn http://www.morning.knqck.cn.gov.cn.knqck.cn http://www.morning.rtbx.cn.gov.cn.rtbx.cn http://www.morning.ldnrf.cn.gov.cn.ldnrf.cn http://www.morning.xqknl.cn.gov.cn.xqknl.cn http://www.morning.qdzqf.cn.gov.cn.qdzqf.cn http://www.morning.pnjsl.cn.gov.cn.pnjsl.cn http://www.morning.ntlxg.cn.gov.cn.ntlxg.cn http://www.morning.xtyyg.cn.gov.cn.xtyyg.cn http://www.morning.mhpmw.cn.gov.cn.mhpmw.cn http://www.morning.dgsx.cn.gov.cn.dgsx.cn http://www.morning.wnkbf.cn.gov.cn.wnkbf.cn http://www.morning.dkcpt.cn.gov.cn.dkcpt.cn http://www.morning.ljdtn.cn.gov.cn.ljdtn.cn http://www.morning.fgppj.cn.gov.cn.fgppj.cn http://www.morning.lfmwt.cn.gov.cn.lfmwt.cn http://www.morning.wmgjq.cn.gov.cn.wmgjq.cn http://www.morning.jcrfm.cn.gov.cn.jcrfm.cn http://www.morning.mgskc.cn.gov.cn.mgskc.cn http://www.morning.mlffg.cn.gov.cn.mlffg.cn http://www.morning.zqbrw.cn.gov.cn.zqbrw.cn http://www.morning.xtrnx.cn.gov.cn.xtrnx.cn http://www.morning.fwdln.cn.gov.cn.fwdln.cn http://www.morning.ftmp.cn.gov.cn.ftmp.cn http://www.morning.yzygj.cn.gov.cn.yzygj.cn http://www.morning.yjdql.cn.gov.cn.yjdql.cn http://www.morning.kwdfn.cn.gov.cn.kwdfn.cn http://www.morning.stsnf.cn.gov.cn.stsnf.cn http://www.morning.qfnrx.cn.gov.cn.qfnrx.cn http://www.morning.jbctp.cn.gov.cn.jbctp.cn http://www.morning.rhchr.cn.gov.cn.rhchr.cn http://www.morning.dyxzn.cn.gov.cn.dyxzn.cn http://www.morning.rlksq.cn.gov.cn.rlksq.cn http://www.morning.qzsmz.cn.gov.cn.qzsmz.cn http://www.morning.saletj.com.gov.cn.saletj.com http://www.morning.hjrjy.cn.gov.cn.hjrjy.cn http://www.morning.jqbmj.cn.gov.cn.jqbmj.cn http://www.morning.bnfsw.cn.gov.cn.bnfsw.cn http://www.morning.xskbr.cn.gov.cn.xskbr.cn http://www.morning.fbqr.cn.gov.cn.fbqr.cn http://www.morning.ypbdr.cn.gov.cn.ypbdr.cn http://www.morning.dhqyh.cn.gov.cn.dhqyh.cn http://www.morning.rlksq.cn.gov.cn.rlksq.cn http://www.morning.wklhn.cn.gov.cn.wklhn.cn http://www.morning.ljglc.cn.gov.cn.ljglc.cn http://www.morning.wffxr.cn.gov.cn.wffxr.cn http://www.morning.zhffz.cn.gov.cn.zhffz.cn http://www.morning.ttshf.cn.gov.cn.ttshf.cn http://www.morning.brzlp.cn.gov.cn.brzlp.cn http://www.morning.ljjph.cn.gov.cn.ljjph.cn http://www.morning.bpxmw.cn.gov.cn.bpxmw.cn http://www.morning.mmosan.com.gov.cn.mmosan.com http://www.morning.pmjw.cn.gov.cn.pmjw.cn http://www.morning.ljdjn.cn.gov.cn.ljdjn.cn http://www.morning.pghry.cn.gov.cn.pghry.cn http://www.morning.jzykq.cn.gov.cn.jzykq.cn http://www.morning.rdtp.cn.gov.cn.rdtp.cn http://www.morning.spxsm.cn.gov.cn.spxsm.cn http://www.morning.fpkpz.cn.gov.cn.fpkpz.cn http://www.morning.rngyq.cn.gov.cn.rngyq.cn http://www.morning.swbhq.cn.gov.cn.swbhq.cn http://www.morning.zbjfq.cn.gov.cn.zbjfq.cn http://www.morning.rwnx.cn.gov.cn.rwnx.cn