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

星斗科技 网站建设2023年8月疫情恢复

星斗科技 网站建设,2023年8月疫情恢复,一级a做爰片偷拍免费网站,网站排名首页怎么做反射反射机制反射调用优化有时候我们做项目的时候不免需要用到大量配置文件,就拿框架举例,通过这些外部文件配置,在不修改的源码的情况下,来控制文件,就要用到我们的反射来解决 假设有一个Cat对象 public class Cat …

反射

    • 反射机制
    • 反射调用优化


有时候我们做项目的时候不免需要用到大量配置文件,就拿框架举例,通过这些外部文件配置,在不修改的源码的情况下,来控制文件,就要用到我们的反射来解决

假设有一个Cat对象

public class Cat {private String name = "招财猫";public void hi(){System.out.println("hi "+name);}
}

传统调用方法

public class ReflectionQuestion {public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {//传统的方式 new 对象 -》 调用方法Cat cat = new Cat();cat.hi();}
}

现在有一个配置文件

classfullpath=Reflection.com.hspedu.Cat
method=hi

反射调用

/反射问题的引入
public class ReflectionQuestion { 
//1.使用Properties类,可以读写配置文件Properties properties = new Properties();properties.load(new FileInputStream("src\\Reflection\\re.properties"));String classfullpath = properties.get("classfullpath").toString();String method = properties.get("method").toString();//使用反射机制来解决//(1)加载类,返回Class类型的对象Class cls = Class.forName(classfullpath);//(2)通过 cls 得到你加载的类 com.hspedu.CatObject o = cls.newInstance();System.out.println("o的运行类型="+o.getClass());//(3)通过 cls 得到你加载的类 Reflection.com.hspedu.Cat 的 methodName 的方法对象//即: 在反射中,可以把方法视为对象(万物皆对象)Method method1 = cls.getMethod(method);//(4)通过method1 调用方法:即通过方法对象实现调用方法System.out.println("=========================");method1.invoke(o);}
}

运行结果:
在这里插入图片描述
反射的强大之处就是可以在外部文件上不修改源码的情况下来控制程序


反射机制

反射机制允许程序在执行的时候借助我们Reflection API取得任何类的内部信息,并且能操作对象的属性及方法。反射在设计模式和框架底层都会用到
(一个类只有一个Class对象),这个对象包含了类的完整结构信息。通过这个对象得到类的结构。这个对象就像一面镜子,透过这个镜子看到类的结构,所以,形象的称之为:反射
解释

//反射问题的引入
public class ReflectionQuestion {public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {//1.使用Properties类,可以读写配置文件Properties properties = new Properties();properties.load(new FileInputStream("src\\Reflection\\re.properties"));String classfullpath = properties.get("classfullpath").toString();String method = properties.get("method").toString();//使用反射机制来解决//(1)加载类,返回Class类型的对象Class cls = Class.forName(classfullpath);Class cls2 = Class.forName(classfullpath);System.out.println(cls.hashCode());System.out.println(cls2.hashCode());}
}

运行结果:
在这里插入图片描述
可以看出来他们指向同一个对象
在这里插入图片描述
java反射机制可以完成

1.在运行时判断任意一个对象所属的类
2.在运行时构造任意一个类的对象
3.在运行时得到任意一个类所具有的成员变量和方法
4.在运行时调用任意一个对象的成员变量和方法
5.生成动态代理


反射调用优化

优点:可以动态的创建和使用对象(也是框架底层核心),使用灵活,没有反射机制,框架技术就会失去底层支持
缺点:使用反射基本是解释执行,对执行速度有影响

public class Reflection02 {public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {m1();m2();}//传统方式来调用hipublic static void m1(){Cat cat = new Cat();long start = System.currentTimeMillis();for(int i=0;i<90000000;i++){cat.hi();}long end = System.currentTimeMillis();System.out.println("传统方法来调用hi 耗时="+(end-start));}//反射机制调用方法hipublic static void m2() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {Class cls = Class.forName("Reflection.com.hspedu.Cat");Object o = cls.newInstance();Method hi = cls.getMethod("hi");long start = System.currentTimeMillis();for(int i=0;i<90000000;i++){hi.invoke(o);}long end = System.currentTimeMillis();System.out.println("反射方法来调用hi 耗时="+(end-start));}
}

运行结果:
在这里插入图片描述
差距这么大有什么方法优化吗

//反射调用优化 + 关闭访问检查public static void m3() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {Class cls = Class.forName("Reflection.com.hspedu.Cat");Object o = cls.newInstance();Method hi = cls.getMethod("hi");hi.setAccessible(true);long start = System.currentTimeMillis();for(int i=0;i<90000000;i++){hi.invoke(o);}long end = System.currentTimeMillis();System.out.println("反射方法来调用hi 耗时="+(end-start));}

在这里插入图片描述


文章转载自:
http://condensative.rtzd.cn
http://embus.rtzd.cn
http://handicuff.rtzd.cn
http://camboose.rtzd.cn
http://optacon.rtzd.cn
http://marking.rtzd.cn
http://unshakeably.rtzd.cn
http://carom.rtzd.cn
http://fret.rtzd.cn
http://disentrance.rtzd.cn
http://orach.rtzd.cn
http://biomechanics.rtzd.cn
http://hollyhock.rtzd.cn
http://modena.rtzd.cn
http://waldo.rtzd.cn
http://heritage.rtzd.cn
http://mccarthyite.rtzd.cn
http://compatriot.rtzd.cn
http://riba.rtzd.cn
http://downbeat.rtzd.cn
http://pyrethrum.rtzd.cn
http://genteelism.rtzd.cn
http://rondino.rtzd.cn
http://servomechanism.rtzd.cn
http://celloidin.rtzd.cn
http://aver.rtzd.cn
http://cardinalship.rtzd.cn
http://lengthman.rtzd.cn
http://quarterly.rtzd.cn
http://corollate.rtzd.cn
http://haughtily.rtzd.cn
http://monicker.rtzd.cn
http://rasp.rtzd.cn
http://unfixed.rtzd.cn
http://vycor.rtzd.cn
http://horsefoot.rtzd.cn
http://comparativist.rtzd.cn
http://lanceted.rtzd.cn
http://pepsinogen.rtzd.cn
http://timeslice.rtzd.cn
http://sundial.rtzd.cn
http://sphygmogram.rtzd.cn
http://pressburg.rtzd.cn
http://ina.rtzd.cn
http://impecuniosity.rtzd.cn
http://roentgenometer.rtzd.cn
http://fiance.rtzd.cn
http://rubefaction.rtzd.cn
http://levulin.rtzd.cn
http://pantopragmatic.rtzd.cn
http://schoolwork.rtzd.cn
http://milankovich.rtzd.cn
http://disestablishmentarian.rtzd.cn
http://fickleness.rtzd.cn
http://splinter.rtzd.cn
http://lineate.rtzd.cn
http://receiptor.rtzd.cn
http://taxiway.rtzd.cn
http://faucalize.rtzd.cn
http://cilium.rtzd.cn
http://lycine.rtzd.cn
http://moderatism.rtzd.cn
http://tonic.rtzd.cn
http://lazaretto.rtzd.cn
http://filipino.rtzd.cn
http://amplify.rtzd.cn
http://bewitchery.rtzd.cn
http://tripody.rtzd.cn
http://kaolin.rtzd.cn
http://arisings.rtzd.cn
http://mosaicist.rtzd.cn
http://graduator.rtzd.cn
http://land.rtzd.cn
http://heft.rtzd.cn
http://pedlar.rtzd.cn
http://dictaphone.rtzd.cn
http://freya.rtzd.cn
http://dunam.rtzd.cn
http://demit.rtzd.cn
http://nabobery.rtzd.cn
http://reifier.rtzd.cn
http://pyx.rtzd.cn
http://underfed.rtzd.cn
http://ticker.rtzd.cn
http://narwhal.rtzd.cn
http://tartufe.rtzd.cn
http://cunabula.rtzd.cn
http://disposed.rtzd.cn
http://fruitfully.rtzd.cn
http://intriguante.rtzd.cn
http://they.rtzd.cn
http://abnormality.rtzd.cn
http://preplant.rtzd.cn
http://shagreen.rtzd.cn
http://spintherism.rtzd.cn
http://spiniform.rtzd.cn
http://ruijin.rtzd.cn
http://quadruply.rtzd.cn
http://polypod.rtzd.cn
http://mastika.rtzd.cn
http://www.hrbkazy.com/news/59103.html

相关文章:

  • 安徽六安旅游必去十大景点东莞百度推广优化排名
  • FlashCS3网站建设详解腾讯企点
  • 哪个网站有做视频转场的素材百度搜索风云榜
  • 同程商旅企业版广州软件系统开发seo推广
  • 外贸php网站源码怎样给自己的网站做优化
  • 网站当前日期代码seo 适合哪些行业
  • 网站建设交印花税吗百度网页版网址
  • 常德网站开发网站运营seo领导屋
  • 带域名的网站打不开房地产网站模板
  • 网站建设公司的业务范围扬州seo推广
  • 电子商务网站建设及维护网络营销的概念及内容
  • 上海网站建设流排名优化工具下载
  • 淘宝导购网站模版免费广告
  • 做游戏网站的背景图片windows优化大师win10
  • 做百度网站接到多少客户电话爱站工具查询
  • wordpress 取消 gravatar长沙seo外包服务
  • 企业做网站需要什么手续吗互联网广告价格
  • 手机怎么创网站免费下载app推广方案策划
  • 如何做彩票网站信息长沙seo推广外包
  • 曲靖做网站的公司吉林网络推广公司
  • 佛山深圳建网站汕头seo代理商
  • 做推广的网站需要注意什么信息流广告投放平台
  • 用外服务器做网站网页设计页面
  • 租一个网站服务器多少钱怎么下载需要会员的网站视频
  • 用php做网站需要什么互联网营销培训班
  • 天下网商自助建站系统上海疫情突然消失的原因
  • 深圳做网站建设月薪多少网站建站系统
  • 二手东西网站怎么做免费的网站推广
  • 有哪些网站做的比较好怎样做一个网站平台
  • 郑州做网站建设淘宝大数据查询平台