1.注解和注释
注解:给程序看的.
注释:给开发者看的.
2.元注解
作用:负责注解其他注解,Java中有四个标准meta-annotation类型他们被用来提供对其他annotation类型说明.
@Target:描述注解使用范围
@Retention:表示需要子啊什么级别保存该注释信息,描述注解生命周期
(SOURCE<CLASS<RUNTIME)
@Document:说明该注解将被包含在javaDoc
@Inheritend:说明子类可以继承父类该注解
@MyAnnotation
public class Test01 {
public void test(){
}
}
//表示注解使用的位置
@Target({ElementType.ANNOTATION_TYPE,ElementType.TYPE})
//表示注解的有效位置
@Retention(value = RetentionPolicy.RUNTIME)
//将注解生成在javadoc中
@Documented
//子类可以继承父类
@Inherited
@interface MyAnnotation{
}
3.注解参数
注解内一个参数时推荐使用value,在使用注解时value可以省略.
public class Test2 {
@MyAnnotation1(age = 18)
public void test(){
}
@MyAnnotation2("sss")
public void test2(){
}
}
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation1{
String Name() default "张三";
int age();
}
@Target({ElementType.TYPE,ElementType.METHOD})
@interface MyAnnotation2{
String value();
}
4.反射机制Java.Reflection
Refliction(反射)是Java被视为动态语言的关键,反射机制允许程序在执行期,借助ReflectionAPI取得任何类内部信息,并能直接操作任意对象内属部性及方法.
一个类在内存中只有一个class对象.
创建class类的方式
package annoction;
public class Test04 {
public static void main(String[] args) throws ClassNotFoundException {
Person person=new Student();
System.out.println("这个人:"+person.name);
Class c1=person.getClass();
System.out.println(c1.hashCode());
Class c2=Class.forName("annoction.Person");
System.out.println(c2.hashCode());
Class c3=Person.class;
System.out.println(c3.hashCode());
Class c4 =Integer.TYPE;
System.out.println(c4);
Class c5=c1.getSuperclass();
System.out.println(c5);
}
}
class Person{
String name;
Integer age;
public Person(){
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
class Student extends Person{
public Student() {
this.name="xs";
this.age=10;
}
}
class Teacher extends Person{
public Teacher() {
this.name="老师";
this.age=30;
}
}
5.反射机制获取类的方式
package annoction;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test05 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
Class c1=Class.forName("annoction.User");
System.out.println(c1.getName());//获取包名+类名
System.out.println(c1.getSimpleName());//获取类名
//获取类的属性
Field[] fields=c1.getFields();//public类型的属性
for (Field f :
fields) {
System.out.println("public"+f);
}
Field[] field1=c1.getDeclaredFields();//全部类型的属性
for (Field f :
field1) {
System.out.println("全部"+f);
}
Field name=c1.getDeclaredField("name");//获取指定属性
System.out.println("指定"+name);
//获取本类和父类所有public方法
Method[] methods= c1.getMethods();
for (Method m:methods
) {
System.out.println("public方法"+m);
}
//获取本类所有方法
Method[] methods1= c1.getDeclaredMethods();
for (Method m:methods1
) {
System.out.println("所有方法"+m);
}
//获取指定的方法
Method getName=c1.getMethod("getName",null);
Method setName=c1.getMethod("setName",String.class);
System.out.println(getName);
System.out.println(setName);
//获取指定的构造器
Constructor[] constructors=c1.getConstructors();
for (Constructor c :
constructors) {
System.out.println("构造器"+c);
}
constructors=c1.getDeclaredConstructors();
for (Constructor c:constructors
) {
System.out.println("全部构造器"+c);
}
}
}
6.通过反射构造对象
package annoction;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test09 {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
//获取class
Class c1=Class.forName("annoction.User");
//创造一个对象
User user= (User) c1.newInstance();
System.out.println(user);
//构造器创建对象
Constructor declaredConstructor = c1.getDeclaredConstructor(String.class, Integer.class);
User user1 =(User) declaredConstructor.newInstance("秦将", 18);
System.out.println(user1);
//通过反射调用普通方法
User user3 =(User)c1.newInstance();
Method setName= c1.getDeclaredMethod("setName", String.class);
setName.invoke(user3,"11111111");
System.out.println(user3.getName());
//通过反射操作属性 属性为私有
User user4 =(User)c1.newInstance();
Field name=c1.getDeclaredField("name");
//不能直接操作私有属性,通过关闭程序的安全检测.
name.setAccessible(true);
name.set(user4,"11");
System.out.println(user4.getName());
}
}
7.反射获取注解信息
package annoction;
import java.lang.annotation.*;
import java.lang.reflect.Field;
//反射操作注解
public class Test12 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class aClass = Class.forName("annoction.Student2");
Annotation[] annotations = aClass.getAnnotations();
for (Annotation a :
annotations) {
System.out.println(a);
}
Table1 annotation = (Table1) aClass.getAnnotation(Table1.class);
String value=annotation.value();
System.out.println(value);
Field name = aClass.getDeclaredField("name");
Fieldk annotation1 = name.getAnnotation(Fieldk.class);
System.out.println(annotation1.con());
System.out.println(annotation1.length());
System.out.println(annotation1.type());
}
}
@Table1("001")
class Student2{
@Fieldk(con = "db_id",type = "int",length = 10)
private int id;
@Fieldk(con = "db_age",type = "int",length = 10)
private int age;
@Fieldk(con = "db_name",type = "String",length = 10)
private String name;
public Student2() {
}
public Student2(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "Student2{" +
"id=" + id +
", age=" + age +
", 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;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Table1{
String value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Fieldk{
String con();
String type();
int length();
}