Day17-junit、反射、Properties1.Junit(单元测试框架)@Test
public void test() {
System.out.println("测试Junit");
}2.反射类的加载过程:Demo.class(纯文本文件)--->通过类加载器加载--->Demo.class(Class对象)--->实例化对象 反射就是直接操作Class对象的过程 类名.class 对象.getClass() Class.forName("类全路径名")
通过反射操作构造方法、成员变量、成员方法和Class自己的信息 getName(),getSimpleName()等关于自己的信息 getConstructor ()、getDeclaredConstructor() getField(.....)、getDeclaredField() 成员属性处理 setXxx(对象,需要设置的值...)
getMethod(.....)、getDeclareMethod() invoke(传入对象,参数...):调用方法 如果是静态方法,对象传入null
Declare可以获得私有的,但是私有的成员使用前要调用它setAccessible(true)取出权限 通过反射擦除泛型约束
3.PropertiesHashTable的子类(Map + IO) 使用步骤: 创建对象:Properties prop = new Properties(); 读取配置:prop.load(InputStream)--->把文件的数据读取放入到map集合中 获取数据:prop.getProperty("key")--->从map集合中获取数据
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("fileName.properties");
prop.load(fis);
String value = prop.getProperty("key名称");
System.out.println(value);
}
持久话数据到文件
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
prop.setProperty("key1", "value1");
prop.setProperty("key2", "value2");
prop.store(new FileOutputStream("store.properties"), "My title");
}
|
|