黑马程序员技术交流社区
标题:
张老师框架那节一个小问题
[打印本页]
作者:
yjiang_09
时间:
2014-8-31 15:13
标题:
张老师框架那节一个小问题
package com.sf.day1;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Properties;
public class ReflectTest2 {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
//Collection collection=new HashSet();
InputStream iStream=new FileInputStream("config.properties");
Properties properties=new Properties();
properties.load(iStream);
iStream.close();
String className=properties.getProperty("className");
Class cl=Class.forName(className);
Collection collection=(Collection)cl.newInstance();
ReflectPoint aPoint=new ReflectPoint(3, 4);
ReflectPoint bPoint=new ReflectPoint(4, 4);
ReflectPoint cPoint=new ReflectPoint(5, 4);
ReflectPoint dPoint=new ReflectPoint(3, 4);
collection.add(aPoint);
collection.add(bPoint);
collection.add(cPoint);
collection.add(dPoint);
collection.add(aPoint);
System.out.println(collection.size());
}
}
复制代码
className=java.util.ArrayList
复制代码
看视频好像看完了都明白了,其实一定要自己敲一遍,区别其实很大的,你看我自己敲就出现了好多小问题,比如配置文件config.properties里我自己敲的时候习惯性的敲成了
className="java.util.ArrayList;"结果自然是找不到,然后我又回顾了下视频老师怎么写的,然后我改成了
className=java.util.ArrayList;发现还是不行,还是找不到,我又对比了下,才发现还多了个分号。
所以现在我想问个问题:它这里配置格式是怎么样的?为什么不能加引号,分号。原理是什么呢?谢谢!
作者:
Java黑马
时间:
2014-8-31 15:33
配置格式应该就是键值对的格式,读取的时候只能读取改格式的配置,记住就行了
作者:
怀念黑海岸
时间:
2014-8-31 20:37
properties对象存储数据是以map形式存入的,等号前面表示map中键值对的键 Key,等号后面的表示Value,而以输入路径通过反射来查找这个类的话他是不能识别路径名后面的‘;’的。所以他会出错。
作者:
iefegend
时间:
2014-8-31 21:15
不觉明历
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2