<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">我们一般用properties或者XML文件作为资源存储的文件,现在主要介绍一下对properties的操作</span>
1.我们在src文件下新建一个名称为config的包
2.在config下新建一个file,把它命名为demo.properties
3. 把下面的几段文字复制到demo.properties中
#comments username=oseye
4.新建一个叫RunMain.java的class ,在里面添加textProperties方法如下:
public static void textProperties(){ Properties prop=new Properties(); try { prop.load(RunMain.class.getResourceAsStream("/config/demo.properties")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String username = prop.getProperty("username");//此方法实现了在demo.properties读出属性为username的值 System.out.println("username的值:"+username); }
5.新建一个自己的类,这个类名就随便取了,我建了一个叫People的类,另外需要至少两个类实现通过更改demo.propertie来创建不同的对象,所以我写了People1和People2来继承People:
People
package config; public abstract class People { private String sname; private String sex; public People() { super(); // TODO Auto-generated constructor stub } public People(String sname, String sex) { super(); this.sname = sname; this.sex = sex; } //get&set方法 @Override public String toString() { return "People [sname=" + sname + ", sex=" + sex + "]"; } public abstract void setAge(String age); public abstract void setHigh(String high); }
People1:
package config; public class People1 extends People { private String age ; public People1() { super(); // TODO Auto-generated constructor stub } public People1(String sname, String sex,String age) { super(sname, sex); this.age = age; // TODO Auto-generated constructor stub } //getset方法 @Override public String toString() { return "People1 [name=" + super.getSname()+ ",sex="+super.getSex()+",age=" + age + "]"; } }
People2:
package config; public class People2 extends People { private String high ; public People2() { super(); // TODO Auto-generated constructor stub } public People2(String sname, String sex,String high) { super(sname, sex); this.high = high; // TODO Auto-generated constructor stub } @Override public String toString() { return "People2 [name=" + super.getSname()+ ",sex="+super.getSex()+",high=" + high + "]"; } @Override public void setAge(String age) { // TODO Auto-generated method stub } @Override public void setHigh(String high) { // TODO Auto-generated method stub this.high=high; } }
6.在RunMain中创建一个名称为textAll的方法:
public static void textAll(){ Properties prop=new Properties(); People p = null; try { prop.load(RunMain.class.getResourceAsStream("/config/demo.properties")); Class people = Class.forName("config."+prop.getProperty("People")); p = (People)people.newInstance(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } p.setSex("nv"); p.setSname("xww"); if(p instanceof People1){ p.setAge("20"); }else if(p instanceof People2){ p.setHigh("180"); } System.out.println(p); }
7.修改demo.properties的值,添加上我们要创建的类
People=People2
8.在主方法中运行一下textAll()这个方法,再改成People1再运行一下,两者分别输出各自类的信息
9.至此我们就实现了在文件中读取一个字符串并创建以这个字符为名字的类的对象
时间: 2024-10-08 16:36:03