1.JavaBean及内省相关类的使用
JavaBean(狭义)的定义: 1.私有字段 2.存取字段的公有方法 3.无参构造函数
PropertyDesciptor类的使用:
public static void main(String[] args) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Student student = new Student("Peter");
//通过属性名和对应类类型新建一个PropertyDescriptor对象
PropertyDescriptor pd = new PropertyDescriptor("name", Student.class);
//获取读方法
Method readMethod = pd.getReadMethod();
//调用(get)方法
String name = (String) readMethod.invoke(student, null);
System.out.println(name);
//获取写方法
Method writeMethod = pd.getWriteMethod();
//调用(set)方法
writeMethod.invoke(student, "newName");
}
通过Introspector类获得PropertyDesciptor:
public static void main(String[] args) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
BeanInfo beanInfo = Introspector.getBeanInfo(Student.class);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
System.out.print(propertyDescriptor.getName() + " ");
}
}//output : class id name
2.BeanUtils的使用
BeanUtils通过反射技术来存取一个JavaBean的属性 (BeanUtils默认的存取都是使用字符串)
public static void main(String[] args) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Student student = new Student();
BeanUtils.setProperty(student, "name", "haha");
BeanUtils.setProperty(student, "id", "321");
System.out.println(student);
}//output : id : 321 name : haha
由于BeanUtils默认的存取都是通过字符串,对于某些类型的属性需要注册相关的转换注册器ConvertUtil
对Student类中的Date类型属性添加注册器
自定义的:
public static void main(String[] args) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Student student = new Student();
//注册一个转换器
ConvertUtils.register(new Converter() {
@Override
//参数type为JavaBean中属性的类型
public Object convert(Class type, Object value) {
if( type != null && value != null){
try {
String strBirthday = (String)value;
Date date = new SimpleDateFormat("yyyy-MM-DD").parse(strBirthday);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
}
return null;
}
}, java.util.Date.class);
BeanUtils.setProperty(student, "birthday", "2011-01-02");
System.out.println(student);
}
或者使用BeanUtils提供的:
ConvertUtils.register(new DateLocaleConverter(), java.util.Date.class);
还有StringLocaleConverter......
3.泛型(Generic)
作用:限制集合处理类型,从而把程序运行时可能发生问题转变为编译时的问题。
注意:泛型是提供给javac编译器使用的,它用于限定集合的输入类型,让编译器在源代码级别上,防止非法数据。但是编译生成class文件中不再带有泛型信息(擦除)。
1.基本
ArrayList<E>, E称为泛型类型,且E只能为引用类型,不能使基本数据类型。
2.泛型方法
定义泛型方法 public static <T> void show (T obj){}
3.泛型类
public class BaseDao<T>{}
静态方法中不能使用类定义的泛型(实例化的时候才能确定),要单独定义
4.?通配符
使用?通配符,就只能调用对象与?类型无关的方法, 例如 List<?> list
//不能调用与具体类型有关的方法,如
list.add("aa"); list.add(1);
//可以调用与类型无关的方法,如
list.size(); list.isEmpty();
5.限制通配符
限制上界 List< ? extends Number> (想象Number位于继承树的上面,? 位于下面)
限制下界 List< ? super Integer>
4.xml (extensible markup language)介绍
1.xml是一种以标签为中心的语言,成对标签出现。
2.xml是一种结构和关系数据载体,可以用于计算机间的通信。
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
......
</xml>
5.xml 注释、CDATA、特殊字符、指令
1.注释:
<!-- 注释内容 -->
2.CDATA区
<![CDATA[
原样输出这里的所有内容
]]>
3.特殊字符
大于 <
大于 >
取地址符 &
双引号 "
单引号 '
其他特殊字符 &#xNNNN; N表示四位16进制数
4.处理指令
处理指令形如 <? ?>
使用css
<?xml-stylesheet type="text/css" href="a.css" ?>
6.XML之DTD(Document Type Definition)语法及使用
a.元素类型
<?xml version="1.0"?>
<!ELEMENT root ANY> 表示root标签为任意内容
<!ELEMENT student (name, id)> 表示studet标签下按顺序的有name和id标签
<!ELEMENT student (name | id)> 表示studet标签下name和id任选一个
<!ELEMENT name (#PCDATA)> 表示name标签元素是文本类型,
<!ELEMENT tt EMPTY>
元素类型
EMPTY :表示内容是空,例如<br/><hr/>
ANY : 表示为任意内容
#PCDATA : 文本
b.元素组成关系
<!ELEMENT student (name, id)> 表示studet标签下按顺序的有name和id标签
<!ELEMENT student (name | id)> 表示studet标签下name和id任选一个 (可以嵌套括号...)
<!ELEMENT student (name*, id)> * 表示任意多次 + 表示一次或多次 ? 表示0次或一次 若无则表示出现1次
c.属性定义
<!ATTLIST 元素名
属性名 属性类型 设置说明
...
>
<!ATTLIST student
name CDATA #REQUIRED,
age CDATA #IMPLIED,
country CDATA #FIXED "中国",
city CDATA "深圳"
>
属性类型:
枚举 :<!ATTLIST 肉
品种 ("鸡肉|牛肉|猪肉") #REQUIRED >
ID : 数字下划线开头,对于整个xml唯一
<!ATTLIST
学生 编号 ID #REQUIRED>
设置说明:
#REUQRED : 表示必须设置这个属性
#IMPLIED: 表示可设也可以不设置这个属性
#FIXED: 表示该属性为固定值,且要在xml中给出这个值
默认 : 若设置则使用设置值,否则下使用dtd中提供的默认值
d.实体Entity定义
引用类型的实体:
用于xml文件中相同的文本
<!ENTITY 实体名 文本>
定义在dtd中,使用在xml中, &实体名;
参数类型的实体
用于dtd中相同的文本(元素)
<!ENTITY % 实体名 文本>
定义在dtd中,使用在dtd中, %实体名;
e.使用dtd的两种方式
单独定义dtd文件(xml文件),在定义内容的xml文件中引用该dtd
<?xml version="1.0"?> <!DOCTYPE root SYSTEM "xxx.dtd"> <!--root是文档根节点 这里的SYSTEM和PUBLIC, PUBLIC表示业界公认公有的DTD --> <root> <student> <name>peter</name> </student> <teacher></teacher> </root>
在xml中直接定义
<?xml version="1.0"?> <!DOCTYPE root [ <!ELEMENT root (student, teacher)> ...... ]> <root> <student> <name>peter</name> </student> <teacher></teacher> </root>