构造函数的反射
1 无参数的构造函数 Class clazz = Class.forName("cn.jauronl.Person");
Constructor constructor = clazz.getConstructor(null);
Person p = c.newInstance(null);
2 Class clazz = Class.forName("cn.jayronl.Person");
Constructor c = clazz.getConstructor(String.class);//取得参数是String的构造函数
Person p = (Person) c.newInstance("aaaaa");
3 可变参数:public Person(String str,int age){....}
Constructor c = clazz.getConstructor(String.class,int.class);
Person p = c.newInstance("xx",34);
4 私有构造方法的反射 private Person(List list){......}
Constructor c = Class.getDeclaredConstructor(List.class);
c.setAccessible(true);//强制反射
Person p = (Person)c.newInstance(new ArrayList());
方法的反射
1 对一般方法进行反射public void aal();
Class clazz = Class.forName("cn.jayronl.Person");
Person p = new Person();
Method method = clazz.getMethod("aal",null);
method.invoke(p,null);//p代表的是反射的哪一个类的方法
2 对有返回值的方法进行反射 public Class[] all(String name,int[] password){....}
Method method = clazz.getMethod("all",String.class,int[].class);
class[] cls = (Class[])method.invoke(p,"aaa",new int[](1,23));
3 反射有参数的方法 public void aal(String name,int password){....}
.....
clazz.getMethod("aal",String.class,int.class);
method.invoke(p,"xxx",23);
4 私有方法的反射 private void aal(InputStream in){...}
Method method = clazz.getDeclaredMethod("aal",InputStream.class);
method.setAccessible(true);
method.invoke(p,new FileInputStream());
5 静态方法的反射 public static void aal(int num){....}
Class clazz = Class.forName("....");
Method m = clazz.getMethod("aal",int.class);
m.invoke(null,23);
6 反射main方法
Method m = clazz.getMethod("main",String[].class);
m.invoke(null new Object[]{new String[]{"aa","bb","cc"}});
第十六讲 反射类的属性字段
public Class Person{
private String name="aa";
private int password;
private static int page;
Field f = clazz.getField("name");
Person p = new Person();
String name = (string)f.get(p);
得到类型:Field f = clazz.getField("name");
Object obj = f.get(p);
Class type = f.getType();
设置字段的值
f.set(p,"ss");
私有的属性反射
Field f = clazz.getDeclaredField("password");
f.setAccessible(true);
f.get(p);
}
第十七讲:内省操作
javabean的属性
BeanInfo info = Instrospector.getBeanInfo(Person.class);
PropertyDescriptor[] pds = info.getPropertyDescriptors();
for(PropertyDescriptor p:pds){
String name = pds.getName();
}
操纵bean的指定属性age:
PeropertyDescriptor pd = new PropertyDescriptor("age",Person.class);
Method m = pd.getWriteMethod();//得到person的setter方法
m.invoke(p,45);//等同与执p.setAge(45);
获取bean的属性值
Method m = pd.getReader();
int i = m.invoke(p,null);
第二三天 xml语言的作用
保存数据,软件配置
DOM和sax解析方法的区别
1dom解析的优点是文档crud比较方便,缺点是占用内存大
2sax优点是占用内存小,解析速度快,只适合读取不适合CRUD(增删改查)
第八讲使用jaxp
1创建工厂:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
2 得到DOM解析器
DocumentBuilder builder = factory.newDocumentBuilder();
3解析XML文档得到代表文档的document
Document document = builder.parse("src/book.xml");
NodeList list = document.getElementByTagName("书名");
Node node = list.item(0);
String content = node.getTextContent();
2节点:
Node root = d.getElementByTagName("书架");
public void list(Node root){
NodeList list = node.getChildNodes();
for(int i=0;i<list.length;i++){
Node child = list.item(i);
}
}
3得到标签属性:
Node bookname = document.getElementByTagName("书名").item(0);
String value = bookname.getAttribute("name");
4添加节点:
Document d = builder.parse("src/book.xml");
Element price = d.createElement("售价");
price.setTextContent("59.00");
//把创建的节点挂到第一本书上
Element book = (Element)d.getElementTagName("书").item(0);
book.appendChild(price);