当前位置: 首页 > news >正文

寻找网站设计与制作深圳网络推广代运营

寻找网站设计与制作,深圳网络推广代运营,外链发布平台有哪些,做防水网站运行java代码要经历的三个阶段 反射,程序框架设计的灵魂,将类的各个组成部分封装成其他对象,这就是反射机制。 框架:半成品的软件,在框架的基础上进行开发,可以简化编码 反射的好处: 可以在…
  • 运行java代码要经历的三个阶段

    在这里插入图片描述

  • 反射,程序框架设计的灵魂,将类的各个组成部分封装成其他对象,这就是反射机制。

  • 框架:半成品的软件,在框架的基础上进行开发,可以简化编码

  • 反射的好处:

    1. 可以在程序运行的过程中,操作这些对象
    2. 可以解耦,提高程序的可拓展性
  • 在运行状态中:

    1. 对于任意一个类,都能够知道这个类的所有属性和方法
    2. 对于任意一个对象,都能够调用它的任意属性和方法
  • 与反射机制相关的类

    1. Java.lang.class :代表整个类编译得到的class文件(字节码)
    2. Java.lang.reflect.Method :代表字节码中的方法字节码
    3. Java.lang.reflect.Constructor :代表字节码中的构造器字节码
    4. Java.lang.reflect.Field :代表字节码中的属性字节码
  • 要操作一个字节码,首先要得到这个类的字节码,下面是三种得到字节码的方式

    1. static Class.forName(String className) 这里的className需要时类的全路径,这是一个静态方法

      public class reflect1 {public static void main(String[] args) throws ClassNotFoundException {//第一种方式Class c1=Class.forName("java.lang.String");System.out.println(c1);}
      }
    2. java中的任意一个对象.getClass() ,这个方法继承自Object

      public class reflect1 {public static void main(String[] args) throws ClassNotFoundException {//第一种方式Class c1=Class.forName("java.lang.String");System.out.println(c1);//第二种方式String s="hello";Class c2=s.getClass();System.out.println(c2);}
      }
    3. java语言中的任意一种类型,包括基本数据类型,都有.class属性。

      public class reflect1 {public static void main(String[] args) throws ClassNotFoundException {//第一种方式Class c1=Class.forName("java.lang.String");System.out.println(c1);//第二种方式String s="hello";Class c2=s.getClass();System.out.println(c2);//第三种方式Class c3=String.class;System.out.println(c3);}
      }
    4. JVM在加载一个类的时候,会把这个类的.class字节码文件放在方法区中,栈中放入main方法进行压栈,堆中new出对象c,但是无论使用什么方法获取字节码,一样的class字节码文件只会放一份在方法区中,得到的c变量都是指向方法区中的同一份.class文件。

  • 使用反射机制来实例化对象

  1. 通过Class的newInstance() 方法可以实例化对象

  2. 但是newInstance()方法内部实际上调用了无参构造方法,必须要保证无参构造的存在才可以

        private static void test2() throws InstantiationException, IllegalAccessException {Object o=Student.class.newInstance();System.out.println(o);}
    
  • 反射机制的灵活性,在不修改源码的情况下,可以做到不同对象的实例化,符合OCP开闭原则:对拓展开放,对修改关闭.

    1. 下面的代码中首先利用FileReader读取配置文件,配置文件里面保存了键和值,值是类名,之后new了一个Properties对象,Properties对象的键和值都是String类型,是一种Map数据类型,之后将文件流对象传入Properties对象,再根据键得到值,然后利用反射机制传入类名新建一个对象,实现了对象的灵活创建。
        private static void test3() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {FileReader fr=new FileReader("Student.properties");Properties pro=new Properties();pro.load(fr);fr.close();String forName=pro.getProperty("Student");Class C=Class.forName(forName);Object o=C.newInstance();System.out.println(o);}
    
  • 如果只想要让类里面的某些代码被执行,那么可以使用静态代码和Class.forName,像下面的代码那样,每次类加载器加载一个类的时候,就会先执行静态代码块,使用Class.forName而不用创建类的实例化变量

    public class reflect1 {static {System.out.println("静态代码执行!");}
    }
    
        private static void test4() {try {Class.forName("reflect1");} catch (ClassNotFoundException e) {throw new RuntimeException(e);}}
    
  • 获取文件的类路径,可以通过类加载器获取,具体如下:

        private static void test4() {String path=Thread.currentThread().getContextClassLoader().getResource("a.txt").getPath();System.out.println(path);}
    
  • 资源绑定器

    1. 用来获取属性配置文件中的内容

    2. 属性配置文件必须要放在类路径下(src下面)

    3. 必须是properties后缀

    4. 绑定某个属性配置文件的时候,只需要填写配置文件的名字,不需要写后缀,也不能写后缀

          private static void test4() {ResourceBundle bundle=ResourceBundle.getBundle("Student");String path=bundle.getString("Student");System.out.println(path);}
      
  • 获取并操作属性字节码

    1. 首先获取整个字节码

    2. 利用字节码的getFields可以获取到public修饰的所有属性

    3. 利用字节码的getDeclaredFields可以获取到所有属性,无论什么修饰符都可以获取到

    4. 遍历属性列表

    5. 通过属性.getName()可以获取到属性的名字

    6. 通过属性.getType()可以获取到属性的完整类型,getSimpleName是获取简化的类名

    7. 通过属性.getModifiers()可以获取到属性的修饰符id,通过Modifier.toString(id)就可以拿到完整的修饰符

      import java.io.Serializable;public class Student implements Serializable {
      //    属性设置为privateprivate String name;private int age;private static String room;//无参数构造public Student() {}//有参数构造public Student(String name, int age, String room) {this.name = name;this.age = age;this.room=room;}//getter方法public String getName() {return name;}public int getAge() {return age;}public static String getRoom() {return room;}//setter方法public void setName(String name) {this.name = name;}public void setAge(int age) {this.age = age;}public static void setRoom(String room) {Student.room = room;}//重写equals方法public boolean equals(Object obj){if(obj instanceof Student) {//首先将得到的Object转为Student,否则使用不了Student特有的方法(多态的弊端)Student t = (Student) obj;return t.getAge() == this.getAge() && t.getName() == this.getName() && t.getRoom() == this.getRoom();}return false;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}
      }
          private static void test5() throws ClassNotFoundException {//获取整个类Class studentClass=Student.class;//获取public修饰的属性,一个Filed包括private static String room;Field[] fileds=studentClass.getFields();for (Field filed : fileds) {System.out.println(filed);}//获取所有的属性,一个Filed包括private static String room;fileds=studentClass.getDeclaredFields();for (Field filed : fileds) {//获取属性的名字System.out.println("属性的名字:");System.out.println(filed.getName());//获取属性的类型Class filedTypeClass=filed.getType();//getName是获取完整类型,getSimpleName是获取简化的类名String filedType=filedTypeClass.getSimpleName();System.out.println("属性的类型");System.out.println(filedType);//获取属性的修饰符int modify=filed.getModifiers();String modifyName= Modifier.toString(modify);System.out.println("属性的修饰符");System.out.println(modifyName);System.out.println("==================");}}
      
  • 利用反射机制访问一个对象的属性

    1. 要使用反射,当然要先获取一个类的字节码文件

    2. 利用获取到的字节码文件,再用newInstance方法创建一个对象(使用反射的类必须有无参构造)

    3. 利用字节码文件获取这个类的某个属性

    4. 利用这个属性给创建的对象的对应的属性赋值

    5. 在设置对象的属性值之前,需要在使用 set 方法之前,调用 setAccessible(true) 方法来取消对字段的访问权限检查。否则, 会报IllegalAccessException 异常。这是因为 name 字段被声明为 private,无法直接访问。但是使用反射机制绕过访问权限限制可能会导致安全问题。

          private static void test6() throws NoSuchFieldException, InstantiationException, IllegalAccessException {//获取整个类Class studentClass=Student.class;Field filed=studentClass.getDeclaredField("name");Object obj=studentClass.newInstance();filed.setAccessible(true); // 取消对字段的访问权限检查filed.set(obj,"zs");System.out.println(obj);}
      
  • 利用反射机制获取Method

    1. 获取整个类的字节码文件

    2. 通过字节码文件得到方法的字节码,并且遍历它

    3. 通过.getName()获取方法名字

    4. 通过.getReturnType().getSimpleName()获取返回值类型

    5. 通过.getModifiers()获取修饰符id,然后通过Modifier.toString(id)得到修饰符

    6. 通过.getParameterTypes()得到修饰符列表,遍历这个列表,通过.getSimpleName()得到参数名称

          private static void test7() {Class studentClass=Student.class;Method[] methods=studentClass.getDeclaredMethods();for (Method method : methods) {//获取方法名System.out.println("方法名");System.out.println(method.getName());//获取方法的返回值类型System.out.println("方法的返回值类型");System.out.println(method.getReturnType().getSimpleName());//获取修饰符列表System.out.println("方法的修饰符列表");System.out.println(Modifier.toString(method.getModifiers()));//方法的参数列表System.out.println("方法的参数列表");Class[] parametorType= method.getParameterTypes();for (Class aClass : parametorType) {System.out.println(aClass.getSimpleName());}System.out.println("========================");}}
      
  • 通过反射机制操纵Method

    1. 首先获取字节码

    2. 利用得到的字节码新建一个对象

    3. 利用得到的字节码获取一个指定的方法,在java里面,由于存在方法的重载,所以区分一个方法是由方法名+方法参数类型,而不仅仅是方法名,所以使用getDeclaredMethod方法需要一个方法名和若干参数的class

    4. 最后使用这个方法的invoke方法,传入要操纵的对象和实参

          private static void test8() throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {Class studentClass=Student.class;Object obj=studentClass.newInstance();Method setName=studentClass.getDeclaredMethod("setName",String.class);Object returnVa=setName.invoke(obj,"zs");System.out.println(obj);}
      
  • 通过反射机制获取构造方法

    1. 获取整个类的字节码文件

    2. 通过字节码文件得到构造方法的字节码,并且遍历它

    3. 通过.getName()获取方法名字

    4. 通过.getModifiers()获取修饰符id,然后通过Modifier.toString(id)得到修饰符

    5. 通过.getParameterTypes()得到修饰符列表,遍历这个列表,通过.getSimpleName()得到参数名称

          private static void test9() {Class studentClass=Student.class;Constructor[] constructors=studentClass.getDeclaredConstructors();for (Constructor constructor : constructors) {//获取构造方法名System.out.println("构造方法名");System.out.println(constructor.getName());//获取修饰符列表System.out.println("构造方法的修饰符列表");System.out.println(Modifier.toString(constructor.getModifiers()));//方法的参数列表System.out.println("构造方法的参数列表");Class[] parametorType= constructor.getParameterTypes();for (Class aClass : parametorType) {System.out.println(aClass.getSimpleName());}System.out.println("========================");}}
      
  • 使用反射机制的构造方法创建对象

    1. 直接利用字节码创建(已过时)

    2. 首先获取无参构造函数,然后调用无参构造函数

    3. 首先获取有参构造函数,然后调用有参构造函数

          private static void test10() throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {//使用反射创建对象//1. 直接利用字节码创建Class studentClass=Student.class;Object t1=studentClass.newInstance();//2. 先利用字节码获取构造函数,然后再利用构造函数创建对象//首先是无参构造Constructor con1=studentClass.getDeclaredConstructor();Object t2=con1.newInstance();//最后是有参构造方法Constructor con2=studentClass.getDeclaredConstructor(String.class,int.class,String.class);Object t3=con2.newInstance("zs",18,"h111");System.out.println(t1);System.out.println(t2);System.out.println(t3);}
      
  • 使用反射获取一个类的父类以及接口

        private static void test11() {Class stringClass=String.class;//获取String的父类Class superClass=stringClass.getSuperclass();System.out.println("父类是");System.out.println(superClass.getSimpleName());//获取接口Class[] interface1=stringClass.getInterfaces();System.out.println("接口是");for (Class aClass : interface1) {System.out.println(aClass.getSimpleName());}}
    

文章转载自:
http://dinncocostal.bkqw.cn
http://dinncorecondition.bkqw.cn
http://dinncohematothermal.bkqw.cn
http://dinncoharebell.bkqw.cn
http://dinncoadonai.bkqw.cn
http://dinncotoilworn.bkqw.cn
http://dinncohincty.bkqw.cn
http://dinncospiritualistic.bkqw.cn
http://dinncoundisciplined.bkqw.cn
http://dinncotheatregoer.bkqw.cn
http://dinncohexaemeron.bkqw.cn
http://dinncoreplevin.bkqw.cn
http://dinncocavernous.bkqw.cn
http://dinncodivorcee.bkqw.cn
http://dinncovesper.bkqw.cn
http://dinncogiessen.bkqw.cn
http://dinncorotund.bkqw.cn
http://dinncolyssophobia.bkqw.cn
http://dinncoent.bkqw.cn
http://dinncoimmingle.bkqw.cn
http://dinncocoalbox.bkqw.cn
http://dinncofixation.bkqw.cn
http://dinncodishevel.bkqw.cn
http://dinncosamdwich.bkqw.cn
http://dinncosericicultural.bkqw.cn
http://dinncorei.bkqw.cn
http://dinncoretroflex.bkqw.cn
http://dinncodivertingness.bkqw.cn
http://dinncofootsore.bkqw.cn
http://dinncoinvoluted.bkqw.cn
http://dinncopmpo.bkqw.cn
http://dinncowarmish.bkqw.cn
http://dinnconauch.bkqw.cn
http://dinncolegitimize.bkqw.cn
http://dinncoamusingly.bkqw.cn
http://dinncomicrotext.bkqw.cn
http://dinncoanion.bkqw.cn
http://dinncotrigonal.bkqw.cn
http://dinncocindy.bkqw.cn
http://dinncocapataz.bkqw.cn
http://dinncovanquish.bkqw.cn
http://dinncocoacervate.bkqw.cn
http://dinncopointy.bkqw.cn
http://dinncoreloan.bkqw.cn
http://dinncourology.bkqw.cn
http://dinncoanam.bkqw.cn
http://dinncodikey.bkqw.cn
http://dinncofrancophobe.bkqw.cn
http://dinnconascence.bkqw.cn
http://dinncosomnus.bkqw.cn
http://dinncodisbelieve.bkqw.cn
http://dinncoeponym.bkqw.cn
http://dinncoinkslinger.bkqw.cn
http://dinncoprepose.bkqw.cn
http://dinnconapoleonic.bkqw.cn
http://dinncomedlar.bkqw.cn
http://dinncodauphine.bkqw.cn
http://dinncoquindecennial.bkqw.cn
http://dinncodonate.bkqw.cn
http://dinncotroublesomely.bkqw.cn
http://dinncopiliated.bkqw.cn
http://dinncodisfranchisement.bkqw.cn
http://dinncohypergeusesthesia.bkqw.cn
http://dinncowinless.bkqw.cn
http://dinncolocomotive.bkqw.cn
http://dinncodosimetry.bkqw.cn
http://dinncolegionaire.bkqw.cn
http://dinncocatenaccio.bkqw.cn
http://dinncop.bkqw.cn
http://dinncolathyritic.bkqw.cn
http://dinncounjoined.bkqw.cn
http://dinncopolarizer.bkqw.cn
http://dinncoejaculator.bkqw.cn
http://dinncoquixotic.bkqw.cn
http://dinncoanticlerical.bkqw.cn
http://dinncokatathermometer.bkqw.cn
http://dinncosnivel.bkqw.cn
http://dinncocolicine.bkqw.cn
http://dinncofecal.bkqw.cn
http://dinncotriclad.bkqw.cn
http://dinncomackintosh.bkqw.cn
http://dinncogibbose.bkqw.cn
http://dinncocharacterisation.bkqw.cn
http://dinncolawyeress.bkqw.cn
http://dinncounhandsome.bkqw.cn
http://dinncobryozoan.bkqw.cn
http://dinncolocalizable.bkqw.cn
http://dinncoaeroflot.bkqw.cn
http://dinncominnie.bkqw.cn
http://dinncounenlightening.bkqw.cn
http://dinncotoadeater.bkqw.cn
http://dinncodescendiblity.bkqw.cn
http://dinncopraelector.bkqw.cn
http://dinnconorth.bkqw.cn
http://dinncobrutalitarian.bkqw.cn
http://dinncoasynergy.bkqw.cn
http://dinncotwyer.bkqw.cn
http://dinncopapeterie.bkqw.cn
http://dinncoknown.bkqw.cn
http://dinncohaste.bkqw.cn
http://www.dinnco.com/news/128685.html

相关文章:

  • 乐清网站制作公司怎么注册电商平台
  • 做网站推销的如何谈客户热点新闻最新消息
  • 建设一个网站的操作流程300字谷歌seo博客
  • 湛江自助建站模板产品线上推广方案
  • 行业门户网站系统网络营销竞价推广
  • 简约创意logo设计免费生成九江seo
  • 网站名词解释厦门网站制作
  • 公司网站包括哪些内容福州短视频seo公司
  • 网站注册域名多少钱完善的seo网站
  • 做pc端网站代理商发帖百度秒收录网站分享
  • 网站线框图网络营销推广8种方法
  • 政府网站新媒体建设方案凡科建站
  • 怎么做网站美工深圳网络推广平台
  • ui设计参考网站有哪些域名ip查询查网址
  • 广州网站的建设承德网络推广
  • 房产中介公司网站源码免费的十大免费货源网站
  • 在线客服源码seo教学视频教程
  • 做推广比较好的网站业务员用什么软件找客户
  • 个人网站域名怎么取快速优化关键词排名
  • 金华大企业网站建设有哪些常见的网络营销工具
  • 成都房地产协会seo系统源码出售
  • 公司做自己的网站平台台站长工具seo综合查询工具
  • 中国住房和建设部厅查询网站南京seo外包平台
  • 做网站什么价格品牌推广的三个阶段
  • 如何做谷歌网站优化全网推广怎么做
  • 开发做游戏的网站腾讯企点app
  • 苏州好的做网站的公司免费舆情监测平台
  • 陕西有色建设有限公司网站免费seo网站优化工具
  • 策划公司怎么找客户seo网站优化推广
  • 网站建设规划书广告网站建设网站排名优化