java封装对象.docx
《java封装对象.docx》由会员分享,可在线阅读,更多相关《java封装对象.docx(11页珍藏版)》请在冰豆网上搜索。
java封装对象
packagecn.itcast.introspector;
importjava.io.BufferedReader;
/*
需求:
编写一个工厂方法根据配置文件的内容,工厂方法返回对应的对象,并且把对象要有对应的属性值。
*/
importjava.io.FileReader;
importjava.lang.reflect.Constructor;
importjava.lang.reflect.Field;
/*
以后我们开发框架的时候,我们是经常需要把一些数据封装到对象中的。
*/
publicclassDemo1{
publicstaticvoidmain(String[]args)throwsException{
Personp=(Person)getInstance();
System.out.println(p);
}
//根据配置文件的内容生产对象的对象并且要把对象的属性值封装到对象中。
publicstaticObjectgetInstance()throwsException{
BufferedReaderbufferedReader=newBufferedReader(newFileReader("obj.txt"));
StringclassName=bufferedReader.readLine();//读取配置文件获取到完整的类名。
Classclazz=Class.forName(className);
//通过class对象获取到无参的构造方法
Constructorconstructor=clazz.getConstructor(null);
//创建对象
Objecto=constructor.newInstance(null);
//读取属性值
Stringline=null;
while((line=bufferedReader.readLine())!
=null){
String[]datas=line.split("=");
//通过属性名获取到对应的Field对象。
Fieldfield=clazz.getDeclaredField(datas[0]);
if(field.getType()==int.class){
field.set(o,Integer.parseInt(datas[1]));
}else{
field.set(o,datas[1]);
}
}
returno;
}
}
反射
packagecn.itcast.introspector;
importjava.beans.BeanInfo;
importjava.beans.IntrospectionException;
importjava.beans.Introspector;
importjava.beans.PropertyDescriptor;
importjava.lang.reflect.Method;
importorg.junit.Test;
/*
内省--->一个变态的反射.
内省主要解决的问题:
把对象的属性数据封装到对象中。
*/
publicclassDemo2{
@Test
publicvoidgetAllProperty()throwsIntrospectionException{
//Introspector内省类
BeanInfobeanInfo=Introspector.getBeanInfo(Person.class);
//通过BeanInfo获取所有的属性描述其
PropertyDescriptor[]descriptors=beanInfo.getPropertyDescriptors();//获取一个类中的所有属性描述器
for(PropertyDescriptorp:
descriptors){
System.out.println(p.getReadMethod());//get方法
}
}
@Test
publicvoidtestProperty()throwsException{
Personp=newPerson();
//属性描述器
PropertyDescriptordescriptor=newPropertyDescriptor("id",Person.class);
//获取属性对应的get或者是set方法设置或者获取属性了。
Methodm=descriptor.getWriteMethod();//获取属性的set方法。
//执行该方法设置属性值
m.invoke(p,110);
MethodreadMethod=descriptor.getReadMethod();//是获取属性的get方法
System.out.println(readMethod.invoke(p,null));
}
}
BeanUtil
packagecn.itcast.introspector;
importjava.lang.reflect.InvocationTargetException;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjavax.xml.crypto.Data;
importmons.beanutils.BeanUtils;
importmons.beanutils.ConvertUtils;
importmons.beanutils.Converter;
/*
BeanUtils:
BeanUtils主要解决的问题:
把对象的属性数据封装到对象中。
BeanUtils的好处:
1.BeanUtils设置属性值的时候,如果属性是基本数据类型,BeanUtils会自动帮我转换数据类型。
2.BeanUtils设置属性值的时候底层也是依赖于get或者Set方法设置以及获取属性值的。
3.BeanUtils设置属性值,如果设置的属性是其他的引用类型数据,那么这时候必须要注册一个类型转换器。
BeanUtilss使用的步骤:
1.导包commons-logging.jar、commons-beanutils-1.8.0.jar
*/
publicclassDemo3{
publicstaticvoidmain(String[]args)throwsException{
//从文件中读取到的数据都是字符串的数据,或者是表单提交的数据获取到的时候也是字符串的数据。
Stringid="110";
Stringname="陈其";
Stringsalary="1000.0";
Stringbirthday="2013-12-10";
//注册一个类型转换器
ConvertUtils.register(newConverter(){
@Override
publicObjectconvert(Classtype,Objectvalue){//type:
目前所遇到的数据类型。
value:
目前参数的值。
Datedate=null;
try{
SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-dd");
date=dateFormat.parse((String)value);
}catch(Exceptione){
e.printStackTrace();
}
returndate;
}
},Date.class);
Empe=newEmp();
BeanUtils.setProperty(e,"id",id);
BeanUtils.setProperty(e,"name",name);
BeanUtils.setProperty(e,"salary",salary);
BeanUtils.setProperty(e,"birthday",birthday);
System.out.println(e);
}
}
输出
编号:
110姓名:
陈其薪水:
1000.0生日:
TueDec1000:
00:
00CST2013
EMP.java
packagecn.itcast.introspector;
importjava.util.Date;
publicclassEmp{
privateintid;
privateStringname;
privatedoublesalary;
privateDatebirthday;
publicDategetBirthday(){
returnbirthday;
}
publicvoidsetBirthday(Datebirthday){
this.birthday=birthday;
}
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicdoublegetSalary(){
returnsalary;
}
publicvoidsetSalary(doublesalary){
this.salary=salary;
}
publicEmp(intid,Stringname,doublesalary){
super();
this.id=id;
this.name=name;
this.salary=salary;
}
publicEmp(){}
@Override
publicStringtoString(){
return"编号:
"+this.id+"姓名:
"+this.name+"薪水:
"+this.salary+"生日:
"+birthday;
}
}
Person.java
packagecn.itcast.introspector;
//实体类---javaBean
publicclassPerson{
privateintid;
privateStringname;
publicPerson(intid,Stringname){
super();
this.id=id;
this.name=name;
}
publicPerson(){}
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
@Override
publicStringtoString(){
return"编号:
"+this.id+"姓名:
"+this.name;
}
}