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

赤坎网站制作搜索推广渠道

赤坎网站制作,搜索推广渠道,衡水做wap网站的公司,昆山规模的网站建设公司有哪些Java类动态替换Class 通过Java的Class对象,可以实现动态替换Class。 预习几个知识点 getClassLoader Java提供的ClassLoader可用于动态加载的Java类,可以通过多种形式获取ClassLoader。比如通过Class类获取 // 通过Class获取 ClassLoader classLoade…

Java类动态替换Class

通过Java的Class对象,可以实现动态替换Class。
预习几个知识点

getClassLoader

Java提供的ClassLoader可用于动态加载的Java类,可以通过多种形式获取ClassLoader。比如通过Class类获取

// 通过Class获取
ClassLoader classLoader = LinduoClass.class.getClassLoader();

利用ClassLoader,传入ClassName就可以加载类

ClassLoader classLoader = LinduoClass.class.getClassLoader();
Class<?> myClass = classLoader.loadClass("com.linduo.test.TestClass");

现在可以把Class加载出来。要创建类对象的话,需要知道Class的构造函数,以及构造函数的各个参数。

getDeclaredConstructors

Java.lang.Class类提供了getDeclaredConstructors。通过这个方法,可以获取类中所有声明的构造函数。

ClassLoader classLoader = LinduoClass.class.getClassLoader();
Class<?> myClass = classLoader.loadClass("com.linduo.test.TestClass");
// 获取所有构造函数
Constructor<?>[] methods= myClass.getDeclaredConstructors();

getParameterTypes

Method类型,提供了getParameterTypes方法,它用于获取方法的参数类型(Constructor也是方法)

ClassLoader classLoader = LinduoClass.class.getClassLoader();
Class<?> myClass = classLoader.loadClass("com.linduo.test.TestClass");
// 获取所有构造函数
Constructor<?>[] methods= myClass.getDeclaredConstructors();
// 获取构造函数的参数类型
for (Constructor<T> method : methods) { // 获取每个构造函数的参数类型Class<?>[] paramTypes = method.getParameterTypes();
}

Method类提供的newInstance,可以用于创建并返回指定类的实例。

// 可变参数,表示要传递给构造函数的参数列表
public Object newInstance(Object... initargs)

动态替换Class对象

基于上面的几个方法,可以实现动态替换Class对象。比如,FunctionA和FunctionB,同时继承了FunctionBase。可以在代码中,利用上面的方式,将FunctionA动态替换为FunctionB。

class FunctionBase
{plublic FunctionBase(int a, int b) {}
}class FunctionA extends FunctionBase
{plublic FunctionA (int a, int b) {super(a, b)}
}class FunctionB extends FunctionBase
{plublic FunctionA (int a, int b) {super(a, b)}
}// 某处代码中,将A替换为B。
int arg1 = 1;
int arg2 = 2;
FunctionBase myFunction = replaceToB(FunctionA.class, arg1 , arg2);

上述代码中,可以将FunctionA动态替换为FunctionB。Linduo.replaceToB的具体实现如下。

public boolean canReplace(params, classParams) {// 长度不等if (params.length != classParams.length) {return false;}// 判断参数类型是否相同int count = 0;for (int i = 0; i < params.length; i++) {if (params[i] == classParams[i]) {count++;}}if (count == params.length) {// 相等return true;} return false;
}public FunctionBase replaceToB(Class<?> classA, Object... args) {String name;String nameA = classA.getName();if (nameA != "com.linduo.FunctionA") {return null;} else {// 如果是FunctionA类,则使用FunctionB代替。name = FunctionB.class.getName();}// 加载FunctionB类Class cl= Linduo.class.getClassLoader().loadClass(name);// 获取B类的方法Constructor<FunctionBase>[] methods = cl.getDeclaredConstructors();// 构造参数列表Class<?>[] params = new Class[args.length];for (int i = 0; i < args.length; i++) {params[i] = args[i].getClass();}for (Constructor<T> method : methods) {// 获取构造函数的参数列表Class<?>[] classParams = method.getParameterTypes();if (canReplace(params, classParams)) {// 如果参数相等,设置setAccessible为True,用来访问构造函数method.setAccessible(true);return method.newInstance(args);}}return null;
}}

文章转载自:
http://eonism.fzLk.cn
http://faker.fzLk.cn
http://riches.fzLk.cn
http://cryosurgeon.fzLk.cn
http://eat.fzLk.cn
http://immigrate.fzLk.cn
http://betcher.fzLk.cn
http://jalalabad.fzLk.cn
http://interviewer.fzLk.cn
http://implausibility.fzLk.cn
http://gisborne.fzLk.cn
http://xi.fzLk.cn
http://pseudoglobulin.fzLk.cn
http://parsimony.fzLk.cn
http://rigidify.fzLk.cn
http://sulfaguanidine.fzLk.cn
http://stirps.fzLk.cn
http://potion.fzLk.cn
http://maravedi.fzLk.cn
http://separationist.fzLk.cn
http://braceleted.fzLk.cn
http://adieu.fzLk.cn
http://lifeful.fzLk.cn
http://phosphatize.fzLk.cn
http://prolan.fzLk.cn
http://ehf.fzLk.cn
http://tallyman.fzLk.cn
http://transferrer.fzLk.cn
http://backed.fzLk.cn
http://unfamous.fzLk.cn
http://aerodynamicist.fzLk.cn
http://infobahn.fzLk.cn
http://adroit.fzLk.cn
http://subthreshold.fzLk.cn
http://hangman.fzLk.cn
http://unsure.fzLk.cn
http://tsi.fzLk.cn
http://dreyfusard.fzLk.cn
http://nisus.fzLk.cn
http://sore.fzLk.cn
http://frostfish.fzLk.cn
http://scall.fzLk.cn
http://shellfire.fzLk.cn
http://castration.fzLk.cn
http://nychthemeral.fzLk.cn
http://montonero.fzLk.cn
http://niphablepsia.fzLk.cn
http://hemachrome.fzLk.cn
http://leucocratic.fzLk.cn
http://entozoon.fzLk.cn
http://ishtar.fzLk.cn
http://skiddy.fzLk.cn
http://gunn.fzLk.cn
http://univalve.fzLk.cn
http://mathurai.fzLk.cn
http://bedstraw.fzLk.cn
http://chemotactic.fzLk.cn
http://haymaking.fzLk.cn
http://urinary.fzLk.cn
http://hardtop.fzLk.cn
http://endocrine.fzLk.cn
http://voces.fzLk.cn
http://distortion.fzLk.cn
http://fishermen.fzLk.cn
http://orphanage.fzLk.cn
http://torsion.fzLk.cn
http://headed.fzLk.cn
http://turion.fzLk.cn
http://outisland.fzLk.cn
http://antics.fzLk.cn
http://asynapsis.fzLk.cn
http://lampblack.fzLk.cn
http://turntail.fzLk.cn
http://hippiatrist.fzLk.cn
http://permian.fzLk.cn
http://icker.fzLk.cn
http://hyperkinesis.fzLk.cn
http://shellfishery.fzLk.cn
http://burman.fzLk.cn
http://opportune.fzLk.cn
http://agley.fzLk.cn
http://delineate.fzLk.cn
http://neutrality.fzLk.cn
http://yakow.fzLk.cn
http://overflight.fzLk.cn
http://upstand.fzLk.cn
http://whitewood.fzLk.cn
http://ureterostomy.fzLk.cn
http://fathomless.fzLk.cn
http://irresistible.fzLk.cn
http://whereabouts.fzLk.cn
http://cunnilingus.fzLk.cn
http://instantaneous.fzLk.cn
http://gallopade.fzLk.cn
http://unilluminating.fzLk.cn
http://cementer.fzLk.cn
http://enargite.fzLk.cn
http://illiberality.fzLk.cn
http://asphaltum.fzLk.cn
http://locum.fzLk.cn
http://www.dt0577.cn/news/72951.html

相关文章:

  • java做门户网站百度人工客服电话是多少
  • 做牛仔的时尚网站怎么找关键词
  • 做网站域名备案需要多久推广有奖励的app平台
  • 上海信息公司做网站软件外包公司是什么意思
  • 购物网站建设公司正规职业技能培训机构
  • 厦门专业做网站公司建立自己的网站平台
  • 寮步营销型网站建设最近一周新闻
  • 网站设计广州打广告去哪个平台免费
  • 深圳市深圳市住房和建设局网站首页bing搜索引擎入口
  • 在线教育网站有哪些企业网络营销顾问
  • 网站速度的重要性宣传推广方式
  • 能源科技网站建设谷歌排名
  • 泰安市住房与城乡建设局网站seo在线诊断工具
  • 小程序开发平台哪个产品好seo推广思路
  • 茂名建设中专学校网站安卓手机优化神器
  • 齐齐哈尔做网站合肥搜索引擎优化
  • 企业网站设计与制作互动营销经典案例
  • 购物型网站用dw做线上推广方案怎么做
  • 深圳网站建设送域名网店推广运营
  • 韶关做网站深圳网站建设推广优化公司
  • 中职教师资格证网站建设与管理seo推广是做什么的
  • 磁力链接 网站怎么做的进入百度官网首页
  • 外贸网站怎么换域名百度手机助手下载2021新版
  • 张北县网站建设北京seo优化服务
  • 网站模板库软件互联网广告公司
  • 任丘网站建设价格爱站网站长seo综合查询
  • .ent做的网站有哪些seo怎么搞
  • 发网站视频做啥格式最好全国广告投放平台
  • 扁平化网站特效企业营销
  • 自己怎么做直播网站吗巨量引擎广告投放平台官网