VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Java教程 >
  • 注解和反射

这两个的概念在Java基础学习中大家就学习了,但很少用到吧,他们主要是为大家学习后面的框架知识打下基础的

注解(Annotation)

首先来说说注解的概念:

我们都知道注释吧,就是给人看的,那注解其实意思差不多,也是一种解释语言,并不是一种程序本身

格式:@注释名;          可以在哪里使用:package、class、method、field相当于给他们添加一些额外的辅助信息,后面我们可以用反射机制编程实现元数据的访问

分类:

内置注解:

@Override    对父类方法的重写

@Deprecated  不推荐程序员使用,但是可以使用

@SuppressWarnings   抑制编译时的警告信息

元注解:

@Target:用于描述注解的可以使用在什么地方

@Retention:一般都用RUNTIME表示在运行时注解(RUNTIME(运行时有效)>class(编译之后有个class有效)>sources(源码级别有效))

@Document:说明该注解将包含在javadoc中

@Inherited:说明子类可以继承父类的该注解

复制代码
import java.lang.annotation.*;
//测试元注解
@MyAnnotationTest
public class AnnotationTest {
    @MyAnnotationTest
    public void add(){

    }
}
//定义一个注解

//@Target:用于描述注解的可以使用在什么地方
@Target(value = {ElementType.METHOD,ElementType.TYPE})//表示我定义的下面的那个MyAnnotationTest注解可以使用在方法和类上面

//@Retention:一般都用RUNTIME表示在运行时注解
@Retention(value = RetentionPolicy.RUNTIME)
//@Document:说明该注解将包含在javadoc中
@Documented
//@Inherited:说明子类可以继承父类的该注解
@Inherited
@interface MyAnnotationTest{//定义一个注解前面要用@interface
}
复制代码

 

自定义注解:

 直接代码展示了:

复制代码
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public class ZdyAnnotation {
//    注解可以显示赋值:如果没有默认值,我们就必须要给注解赋值
    @MyAnnotation(name = "金涛骇浪",array = {"成都理工","电子科大"})
    public void test01(){
    }
    
//    如果只有一个值为value,可以不用写,直接赋值即可
    @MyAnnotation01("金涛海浪")
    public void test02(){

    }
}
//自定义第一个注解
@Target(value = {ElementType.METHOD,ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
@interface MyAnnotation{//定义一个注解前面要用@interface
//    注解的参数:参数类型+参数名();
    String name() default "";
    int age() default 0;
    int id() default -1;//如果默认值为-1,代表不存在
    String[] array();
}
//自定义第二个注解
@Target(value = {ElementType.METHOD,ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
@interface MyAnnotation01{
    String value();
}
复制代码

 

反射(Reflection) 

简单来说就是程序运行时可以改变其结构(Java本身是静态语言,有了反射后,就变成了准动态语言,主要可以获得注解和泛型等等)

 

 

 代码举例:

复制代码
public class FanShe {
    public boolean login(String username,String password){
        if ("admin".equals(username)&&"123".equals(password)){
            System.out.println("登陆成功");
        }else {
            System.out.println("账号或密码错误");
        }
        return false;
    }
}
复制代码

反射调用:

复制代码
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class FanSheTest {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
        //获取类
        Class<?> aClass = Class.forName("com.sep24FanShe.FanShe");
        //获取某个特定的方法

        //通过方法名+形参列表
        Method m = aClass.getDeclaredMethod("login", String.class, String.class);
        //通过反射机制执行login方法
        //用一个Object对象接收它的新的实例
        Object o = aClass.newInstance();
        //调用o对象的m方法 传递 admin 123参数 方法的执行结果是
        Object invoke = m.invoke(o, "admin", "123");//invoke:激活的意思

        System.out.println(invoke);
    }
}




反射操作注解:

复制代码
import java.lang.annotation.*;
import java.lang.reflect.Field;

public class ReflectionAnnotationTest {
//    通过反射操作注解
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class c1 = Class.forName("com.sep24FanShe.Person");
        Annotation[] annotations = c1.getAnnotations();//获得注解的方法
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }
//        获得注解value的值
        hxbClass hxbClass = (hxbClass)c1.getAnnotation(hxbClass.class);//获得指定注解
        String value = hxbClass.value();
        System.out.println(value);
//        获得类指定的注解
        Field id = c1.getDeclaredField("id");
        hxbFiled annotation = id.getAnnotation(hxbFiled.class);
        System.out.println(annotation.columnName());
        System.out.println(annotation.type());
        System.out.println(annotation.length());
    }
}
//写一个javaBean
@hxbClass("db_person")//自定义的类名注解
class Person{
    @hxbFiled(columnName = "db_id",type = "int",length = 10)//自定义的属性注解
    private int id;
    @hxbFiled(columnName = "db_age",type = "int",length = 10)
    private int age;
    @hxbFiled(columnName = "db_name",type = "varchar",length = 3)
    private String name;
    public Person(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }
    @Override
    public String toString() {
        return "Person{" +
                "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;
    }
    public Person() {
    }
}
//定义一个类名注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface hxbClass{
    String value();
}
//定义一个属性注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface hxbFiled{
    String columnName();
    String type();
    int length();
}
复制代码
注解和反射就先说道这里,具体后面框架的时候会用到的,那时再有什么问题再看谢谢大家;饿

 


复制代码



来源:https://www.cnblogs.com/hxbhxb/p/15363010.html


相关教程