元注解
作用:负责注解其他注解
类型:
- @Target:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)
- @Retention:表示需要在什么级别保存该注释信息,用于描述注解的生命周期(SOURCE<CLASS<RUNTIME)
- @Document:说明该注解将被包含在javadoc中
- @Inherited:说明子类可以继承父类中的该注解
package com.dt.tcp;
import java.lang.annotation.*;
@annotation
public class TestAnnotation {
}
//Target表示注解可以用在哪些地方
@Target(value = {ElementType.TYPE})
//Retention 表示注解的适用范围
@Retention(value = RetentionPolicy.RUNTIME)
//Documented 表示是否将注解生成在JAVAdoc中
@Documented
//Inherited 子类可以继承父类的注解
@Inherited
@interface annotation{
}
自定义注解
- @interface 用来声明一个注解,格式:public 注解名{定义内容}
- 其中的每一个方法实际上是声明了一个配置参数
- 方法的名称就是参数的名称
- 返回值类型就是参数的类型
- 可以通过default来声明参数的默认值
- 如果只有一个参数成员,一般参数名为value
- 注解元素必须有值
package com.dt.tcp;
import java.lang.annotation.*;
@annotation("")
public class TestAnnotation {
}
//Target表示注解可以用在哪些地方
@Target(value = {ElementType.TYPE})
//Retention 表示注解的适用范围
@Retention(value = RetentionPolicy.RUNTIME)
//Documented 表示是否将注解生成在JAVAdoc中
@Documented
//Inherited 子类可以继承父类的注解
@Inherited
@interface annotation{
//参数定义方式:类型名+参数名+()
String value();
int id() default 1;
}
反射
- 反射相关的主要API
·java.lang.Class: 代表一个类
·java.lang.reflect.Method:代表类的方法
·java.lang.reflect.Field:代表类的成员变量
·java.lang.reflect.Constructor:代表类的构造器
Class类的常用方法
获得class的几种方式
package com.dt.AnnoReject;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
public class Test01 {
public static void main(String[] args) throws ClassNotFoundException {
Person person = new Student();
//方式一:通过对象获得
Class c1 = person.getClass();
System.out.println(c1.hashCode());
//方式二:forname获得
Class c2 = Class.forName("com.dt.AnnoReject.Student");
System.out.println(c2.hashCode());
//方式三:通过类名.class获得
Class c3 = Student.class;
System.out.println(c3.hashCode());
//方式四:基本内置类型的包装类都有一个Type属性
Class c4 = Integer.TYPE;
System.out.println(c4);
//获得父类类型
Class c5 = c1.getSuperclass();
System.out.println(c5);
}
}
class Person{
String name;
int id;
int age;
}
class Student extends Person{
public Student() {
this.name = "学生";
}
@Override
public String toString() {
return "Student{}";
}
}
class Teacher extends Person{
public Teacher() {
this.name = "老师";
}
}
类加载器的作用
Class对象的作用
- 创建类的对象:调用Class对象的newInstance()方法
·类必须有一个无参数的构造器
·类的构造器访问权限需要足够
package com.dt.AnnoReject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class test02 {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
//获得class对象
Class c1 = Class.forName("com.dt.AnnoReject.Person");
//构造一个对象
Person person = (Person)c1.newInstance();
System.out.println(person);
//通过构造器创建对象
Constructor constructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
Person person1 = (Person) constructor.newInstance("小命", 12, 23);
//通过反射调用普通方法
Person person2 = (Person) c1.newInstance();
Method setName = c1.getDeclaredMethod("setName",String.class);
//invoke:激活
//(对象,方法的值)
setName.invoke(person2, "小红");
//通过反射操作属性
Field name = c1.getField("小张");
//如果属性是私有的
name.setAccessible(true);
}
}
通过反射获得注解
package com.dt.AnnoReject;
import java.lang.annotation.*;
import java.lang.reflect.Field;
public class Test03 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class c1 = Class.forName("com.dt.AnnoReject.Student");
//通过反射获得全部注解
Annotation[] annotation = c1.getAnnotations();
for (Annotation annotation1 : annotation) {
System.out.println(annotation1);
}
//获得注解value的值
Table table = (Table) c1.getAnnotation(Table.class);
String value = table.value();
System.out.println(value);
//获得类指定的注解
Field f = c1.getDeclaredField("name");
FieldTable annotion = f.getAnnotation(FieldTable.class);
System.out.println(annotion.columnName());
System.out.println(annotion.type());
System.out.println(annotion.length());
}
}
@Table("Student")
class Student{
@FieldTable(columnName="name", type = "string", length = 4)
String name;
@FieldTable(columnName="id", type = "string", length = 4)
int id;
@FieldTable(columnName="age", type = "int", length = 10)
int age;
public Student() {
}
public Student(String name, int id, int age) {
this.name = name;
this.id = id;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", id=" + id +
", age=" + age +
'}';
}
}
//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Table{
String value();
}
//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface FieldTable{
String columnName();
String type();
int length();
}