zl程序教程

您现在的位置是:首页 >  后端

当前栏目

[javaSE] 反射-动态加载类详解编程语言

JavaSE反射编程语言 详解 动态 加载
2023-06-13 09:20:37 时间

②请大家区分编译,运行

③编译时刻加载类是静态加载类,运行时刻加载类是动态加载类

 

Ⅰ所有的new对象都是静态加载类

 

在编译的时刻就要去检测该类是否存在,如果不存在,编译失败。

//对于这种情况,静态加载不适用,因为我们需要根据输入来确定加载哪个类

 

package com.tsh.reflect; 

class ReflectLoadDemo { 

 public static void main(String[] args) { 

 if(args[0].equals("Word")){ 

 Word word=new Word(); 

 if(args[0].equals("Excel")){ 

 Excel word=new Excel(); 

}

 

Ⅱ动态加载可以实现当使用的时候才去加载

package com.tsh.reflect; 

class ReflectLoadDemo { 

 public static void main(String[] args) { 

 if(args[0].equals("Word")){ 

 try{ 

 Class c=Class.forName("com.tsh.reflect.Word"); 

 c.newInstance(); 

 }catch(Exception e){ 

 e.printStackTrace(); 

class Word{}

 

 

Ⅲ是代码更具有扩展性的改动,定义interface接口规范

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/12624.html

cjava