总结
我是最棒的!基础不牢,地动山摇!
注解
注解就是程序中的标识,赋予程序中的元素(类,方法,变量,参数…)特殊意义。注解本身是没有任何意义的 ,是需要第三方程序来实现相关的功能。类似于猪肉打上了检疫合格的标志,但是这个标志本身是没有意义的,它的意义是由监管局来认证的,
普通注解
JDK的五个注解
@Override 标识方法重写,例如当前类中重写toString()方法
@Deprecated 标识方法已弃用
@SuppressWarnings 忽略警告
@FunctionalInterface 函数式接口 里面只能有一个抽象方法
@Safevarargs 防止堆污染
元注解
只能标识在注解上的注解,为注解赋予特殊意义
常用元注解
@Target 限制普通注解可以使用在哪些元素上(枚举实现)
@Retention 表示普通注解要保留多久,是Target的前提
- SOURCE 保留到源文件时
- CLASS 保留到编译后
- RUNTIME 保留到运行时,通过反射去获取注解(常用)
@Document Javadoc生成的文档API中可见
@Inherited 子类是否继承父类的注解
自定义注解
package cn.itsource.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface VIP {
String name();
int age();
String[] hobby();
}
package cn.itsource.annotation;
@VIP(name = "xxx",age = 20,hobby = {"c","h","p","d"})
public class Person {
}
package cn.itsource.annotation;
import java.lang.annotation.Annotation;
public class Test {
public static void main(String[] args) throws Exception {
Class<?> clazz = Class.forName("cn.itsource.annotation.Person");
Annotation[] annotations = clazz.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
}
}
XML
W3C定义的XML规范,第一版沿用至今(完美)
自定义标签
CDATA是Character Data的缩写,一般是用来写特殊符号的
DTD约束
Schema约束
DOM模型
D:Document
O:Object
M:Model
XML文件:对象
-
Node 父类 节点对象
-
document 文档对象
-
element 元素对象
-
attribute 属性对象
-
textContent 文本内容对象
XML解析
- DocumentBuilderFactory获取工厂对象
- 获取DocumentBuilder实例
- 解析xml文件(parse方法)得到Document对象
package cn.itsource.xml;
//import java.io.File;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class XmlParseTest {
public static void main(String[] args) throws Exception {
//1.创建DocumentBuilderFactory工厂对象
DocumentBuilderFactory newInstance = DocumentBuilderFactory.newInstance();
//2.通过工厂对象获得DocumentBuilder对象
DocumentBuilder builder = newInstance.newDocumentBuilder();
//3.通过DocumentBuilder对象来解析XML文档得到Document对象
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("contacts.xml");
// Document document = builder.parse(new File("resources/contacts.xml"));
Document document = builder.parse(stream);
System.out.println(document);
}
}