- 框架与框架要解决的核心问题
- 框架调用用户所写的类
- 核心问题:写程序时无法知道要被调用的类的类名,无法直接new某个对象,需要用反射的方式来做
- 综合案例
- 用配置文件加反射方式创建ArrayList或HashSet对象:
import java.util.*; import java.io.*; public class FrameDemo { public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub InputStream is = new FileInputStream("config.properties"); Properties props = new Properties(); props.load(is); is.close(); String className = props.getProperty("className"); Collection col = (Collection)Class.forName(className).newInstance(); col.add(new ReflectPoint(2,3)); col.add(new ReflectPoint(1,4)); col.add(new ReflectPoint(2,3)); System.out.println(col.size()); } }
- 用配置文件加反射方式创建ArrayList或HashSet对象:
07-19
07-19
07-19
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交