加入收藏 | 设为首页 | 会员中心 | 我要投稿 宁德站长网 (https://www.0593zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

关于 Java介绍

发布时间:2021-11-12 16:00:23 所属栏目:教程 来源:互联网
导读:注解是一系列元数据,它提供数据用来解释程序代码,但是注解并非是所解释的代码本身的一部分。注解对于代码的运行效果没有直接影响。--官方文档 元注解: @Retention指定注解存活时间 @Documented @Target指定注解运用的地方 @Inherited当子类没有注解时,父
“注解是一系列元数据,它提供数据用来解释程序代码,但是注解并非是所解释的代码本身的一部分。注解对于代码的运行效果没有直接影响。”--官方文档
 
元注解:
 
@Retention指定注解存活时间
 
@Documented
 
@Target指定注解运用的地方
 
@Inherited当子类没有注解时,父类注解可以被子类继承
 
@Repeatable Java1.8新增注解
 
注解的属性:
 
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    int id() default -1;
    String msg() default "";
}
 
定义注解的属性时,类型必须是8中基本类型、类、接口、注解及他们的数组
 
注解处理器就是通过反射机制获取被检查方法上的注解信息,然后根据注解元素的值进行特定的处理。
反射获取注解:
 
注解在类上
 
//判断类是否有对应TestAnnotation注解
boolean flag = class.isAnnotationPresent(MyAnnotation.class);
//获取对应注解
MyAnnotation myAnnotation = clazz.getAnnotation(MyAnnotation.class);
 
注解在成员变量上
 
Field a = class.getDeclaredField("a");
a.setAccessible(true);
//获取一个成员变量上的注解
MyAnnotation check = a.getAnnotation(MyAnnotation.class);
 
注解在方法上
 
Method testMethod = clazz.getDeclaredMethod("testMethod");
testMethod.isAnnotationPresent(MyAnnotation.class);
testMethod.getAnnotation(MyAnnotation.class);
 
下面记录一个使用注解进行测试的事例,main方法会检查指定注解的方法是否正确实现了排序算法
 
package test;
 
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
/**
 * Created by saishangmingzhu on 2018/8/29.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
    int[] input() default {1,2,3,9,6,7,8,5,4};
    int[] output() default {9,8,7,6,5,4,3,2,1};
}
 
package test;
 
/**
 * Created by saishangmingzhu on 2018/8/29.
 */
public class Sort {
 
    /**
    * 冒泡
    * @param a
    * @return
    */
    @MyAnnotation(input = {1,2,3,9,6,7,8,5,4,11},output = {11,9,8,7,6,5,4,3,2,1})
    public int[] bubbleSort(int[] a){
        int length=a.length;
        for (int i=0;i<length;i++){
            for (int j=i+1;j<length;j++){
                if (a[j]>a[i]){
                    int b=a[i];
                    a[i]=a[j];
                    a[j]=b;
                }
            }
        }
        return a;
    }
 
    /**
    * 选择
    * @param a
    * @return
    */
    @MyAnnotation(input = {1,2,3,9,6,7,8,5,6,4,11},output = {11,9,8,7,6,6,5,4,3,2,1})
    public int[] selectionSort(int[] a){
        int length=a.length;
        for (int i=0;i<length;i++){
            int minIndex=i;
            for (int j=i+1;j<length;j++){
                if (a[minIndex]<a[j]){
                    minIndex=j;
                }
            }
            int b=a[minIndex];
            a[minIndex]=a[i];
            a[i]=b;
        }
        return a;
    }
 
    /**
    * 插入排序
    * @param a
    * @return
    */
    @MyAnnotation
    public int[] insertionSort(int[] a){
        int length=a.length;
        for (int i=1;i<length;i++){
            int tmp=a[i];
            int j=i-1;
            for (;j>=0&&tmp>a[j];j--){
                a[j+1]=a[j];
            }
            a[j+1]=tmp;
        }
        return a;
    }
 
    @MyAnnotation
    public int[] noSort(int[] a){
        return a;
    }
}
 
package test;
 
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
 
/**
 * Created by saishangmingzhu on 2018/8/29.
 */
public class TestSort {
 
    public static void main(String[] arg) throws InvocationTargetException, IllegalAccessException {
        Sort s=new Sort();
        Class sClass=s.getClass();
        //Method method=sClass.getDeclaredMethod("test",int[].class);
        Method[] methods=sClass.getDeclaredMethods();
        for (Method method:methods) {
            if (method.isAnnotationPresent(MyAnnotation.class)) {
                MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
                int[] b = (int[]) method.invoke(s,myAnnotation.input());
                int[] out = myAnnotation.output();
                boolean flag = false;
                if (b.length == out.length) {
                    flag = true;
                    for (int i = 0; i < out.length; i++) {
                        if (b[i] != out[i]) {
                            flag = false;
                            break;
                        }
                    }
                }
                System.out.println(method.getName()+":"+flag);
                if (!flag){
                    for (int i:b){
                        System.out.print(i+",");
                    }
                    System.out.println();
                }
            }
        }
    }
}
 
ps:提一句,循环中最好提前获取数组长度赋值在新变量上,避免每次循环时重复计算获取影响效率。(《Effecive java》)

(编辑:宁德站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读