public class Demo1 {
public static void main(String[] args) throws Exception {
getClazz();
getConstructor();
getMethod();
getField();
}
(1)获取类的类对象
public static void getClazz() throws Exception{
== 方式1:使用class属性获取==
Class> clazz1=Person.class;
System.out.println(clazz1.hashCode());
== 方法2 使用getClass方法==
Person shaobo=new Person();
Class> clazz2 = shaobo.getClass();
System.out.println(clazz2.hashCode());
== 方式3 使用Class.forName方法:耦合性低,不依赖于具体的类,可以编译通过==
Class> clazz3=Class.forName(“com.qf.Person”);
System.out.println(clazz3.hashCode());
//扩展
== 获取父类==
Class> superclass = clazz3.getSuperclass();
System.out.println(superclass.getName());
Type genericSuperclass = clazz3.getGenericSuperclass();
System.out.println(genericSuperclass.getTypeName());
== 获取接口==
Class>[] interfaces = clazz3.getInterfaces();
System.out.println(interfaces.length);
for (Class> anInterface : interfaces) {
System.out.println(anInterface.toString());
}
== 获取包名==
Package aPackage = clazz3.getPackage();
System.out.println(aPackage.getName());
}
public static void getConstructor() throws Exception{
Class> clazz=Class.forName(“com.qf.Person”);
构造方法
Constructor>[] constructors = clazz.getConstructors();
for (Constructor> constructor : constructors) {
System.out.println(constructor);
}
System.out.println("--------------------------");
== //2.1获取无参构造方法==
Constructor> constructor = clazz.getConstructor();
//2.2获取带参
Constructor> constructor1 = clazz.getConstructor(String.class, int.class, String.class);
System.out.println(constructor);
System.out.println(constructor1);
== //3利用构造方法创建对象==
System.out.println("---------利用构造方法创建对象------------");
Person zhangsan=new Person();
Object lisi = constructor.newInstance();
System.out.println(zhangsan);
System.out.println(lisi);
Object wangwu = constructor1.newInstance("王五", 20, "男");
System.out.println(wangwu);
//简单创建对象的方法
Object o = clazz.newInstance();
System.out.println(o.toString());
}
public static void getMethod() throws Exception{
//1获取无参的方法
Class> clazz=Class.forName(“com.qf.Person”);
//Method[] methods = clazz.getMethods();//获取公类自己公开的方法,继承的公开方法
Method[] methods = clazz.getDeclaredMethods();//获取公类自己所有的方法,包括非公开的方法
System.out.println("-----------getMethods();----------");
for (Method method : methods) {
System.out.println(method);
}
Person p=new Person();
p.show();
Object zhangsan = clazz.newInstance();
Method show = clazz.getMethod(“show”);
show.invoke(zhangsan);// zhangsan.show();
2获取有参
Method show2=clazz.getMethod(“show”,String.class);
show2.invoke(zhangsan, “北京”);
== 3获取带返回值的方法==
Method getInfo = clazz.getMethod(“getInfo”);
Object value=getInfo.invoke(zhangsan);
System.out.println(value);
4获取静态方法
Method print = clazz.getMethod(“print”);
print.invoke(null);// Person.print();
5私有方法
Method show3 = clazz.getDeclaredMethod(“show”, String.class, String.class);
//设置访问权限无效
show3.setAccessible(true);
show3.invoke(zhangsan, “上海”,“[email protected]”);
}
public static void getField() throws Exception{
== 1获取类对象==
Class> clazz = Class.forName(“com.qf.Person”);
== 2获取==
Field[] declaredFields = clazz.getDeclaredFields();
for (Field declaredField : declaredFields) {
System.out.println(declaredField);
}
获取单个
System.out.println("--------------------------");
Field name = clazz.getDeclaredField(“name”);
Person wangwu=new Person();
wangwu.name=“王五”;
Object wangwu = clazz.newInstance();
== 赋值==
name.setAccessible(true);
name.set(wangwu, “王五”);// wangwu.name=“王五”;
== 获取==
Object object=name.get(wangwu);// wangwu.name
System.out.println(object);
}
三个基本的 Annotation:
@Override:限定重写父类方法, 该注解只能用于方法
@Deprecated:用于表示某个程序元素(类, 方法等)已过时
@SuppressWarnings: 抑制编译器警告.
注解属性的作用:原来写在配置文件中的信息,可以通过注解的属性进行描述。
Annotation的属性声明方式:String name();
属性默认值声明方式:Stringname() default “xxx”;
特殊属性value:如果注解中有一个名称value的属性,那么使用注解时可以省略value=部分,如@MyAnnotation(“xxx")
特殊属性value[];
创建和使用
public @interface MyAnnocation {
String name();
int num() default 10;
MyAnnocation2 anno();
}
public @interface MyAnnocation2 {
String value();
}
public class Demo1 {
@MyAnnocation(name="哈哈",num=50,anno=@MyAnnocation2(value = "xxx"))
public void show() {
System.out.println("xxxxxxx");
}
}
public class Demo1 {
public static void main(String[] args) throws Exception{
//readXML();
// writeXML();
// updateAndDelete();
readXMLXPath();
}
//(1)读取xml文件
public static void readXML() throws Exception{
//1创建SaxReader读取器(类似 流)
SAXReader reader=new SAXReader();
//2读取,返回Document对象(代表DOM树)
Document document=reader.read(new FileReader("aaa.xml"));
//3获取跟节点
Element root = document.getRootElement();
System.out.println(root);//books
//4获取子节点
List bookList = root.elements("book");
//5遍历
for (Element element : bookList) {
// System.out.println(element);
//获取id属性
String id = element.attributeValue("id");
//获取name
String name = element.elementText("name");
//获取author
String author = element.elementText("author");
//获取price
String price = element.elementText("price");
System.out.println(id+" "+name+" "+author+" "+price);
}
}
//(2)写入xml文件
public static void writeXML() throws Exception{
//1创建SaxReader
SAXReader reader=new SAXReader();
//2读取,获取Document对象
Document document=reader.read(new FileReader("aaa.xml"));
//3获取根节点
Element root = document.getRootElement();
//4添加元素
Element book = root.addElement("book");
//5添加属性
book.addAttribute("id", "1003");
//6添加子元素
book.addElement("name").setText("java基础入门");
book.addElement("author").setText("xxx");
book.addElement("price").setText("100");
//数据都在内存中
//写入books.xml文件中
//1创建输出格式
OutputFormat outputFormat=OutputFormat.createCompactFormat();
outputFormat.setEncoding("utf-8");
//2创建XMLWriter
XMLWriter writer=new XMLWriter(new FileWriter("aaa.xml"), outputFormat);
//3写入
writer.write(document);
//4关闭
writer.close();
}
//(3)修改和删除
public static void updateAndDelete() throws Exception{
//1创建SaxReader
SAXReader reader=new SAXReader();
//2读取,获取Document对象
Document document=reader.read(new FileReader("aaa.xml"));
//3获取根节点
Element root = document.getRootElement();
List bookList = root.elements("book");
//4更新价格
Element firstbook = bookList.get(0);
firstbook.element("price").setText("99");
Element secondbook = bookList.get(1);
secondbook.element("price").setText("8");
//5删除最后一个
Element lastbook = bookList.get(bookList.size() - 1);
root.remove(lastbook);
//6写入文件
OutputFormat outputFormat=OutputFormat.createPrettyPrint();
outputFormat.setEncoding("utf-8");
//7创建XMLWriter
XMLWriter writer=new XMLWriter(new FileWriter("books.xml"), outputFormat);
//8写入
writer.write(document);
//9关闭
writer.close();
}
}