四十一,java中Annotation详解

本文详细介绍了Java中的注解(Annotation)概念及其应用。包括系统内置注解如@Override、@Deprecated、@SupressWarnings的使用场景,自定义注解的定义方法,以及如何利用反射机制获取注解信息等内容。

1.Annotation简介

Annotation实际上表示一种注释的语法,java中最早的程序是提倡代码与配置相分离,而最新的理论又是将所有的配置直接写入到程序中去,那么如果想要完成这样的功能,则需要使用Annotation.

JDK1.5之后的新特性:枚举,自动装箱拆箱,foreach,可变参数,静态导入,泛型.

2.系统内建的Annotation

  • @Override

  • @Deprecated

  • @SupressWarnings

2.1 @Override

@Override表示正确的覆写操作.

示例:

父类代码

public class Person {
    public String say(){
        return "人在说话。" ;
    }
}


子类代码 :
public class Studnt extends Person {
    @Override
    public String say(){
        return "学生在说话" ;
     }
}

@Override可以保证正确的覆写,如果一个方法声明了覆写,而实际上覆写有问题就会提示出错误.


2.2 @Deprecated

@Deprecated表示不建议使用的操作.

示例:

public class Info {
    @Deprecated
    public String getInfo(){
        return "hello" ;
    }
}
getInfo这个方法就是不建议使用的操作 ,在代码中会用中划线表示警告 ,但是不影响实际使用 .


2.3 @SupressWarnings

@SupressWarnings表示压制警告.

示例:

public class TestInfo {

	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		new Info().getInfo() ;
	}

}
注意的是 SupressWarnings可以压制多个警告 .

示例:

import java.io.Serializable;

@SuppressWarnings({ "serial", "deprecation" })
public class Person extends Info implements Serializable {

}


使用过程中,可以明确表示出是为 SuppressWarnings中的value属性赋值.

import java.io.Serializable;

@SuppressWarnings(value = { "serial", "deprecation" })
public class Person extends Info implements Serializable {

}




3.自定义Annotation

定义Annotation的语法:

public @Annotation

示例:

public @interface MyAnnotation {

    public String key() ;
    public String value() ;
}
现在如果要使用此 Annotation,不在同一个包中需要导入 ,导入之后使用 @Annotation的访问语法 .

示例:

@MyAnnotation(key = "ARES", value = "19")
public class Info {
}

注:可以为Annotation定义默认的属性.

public @interface MyAnnotation {
	public String key() default "ARES";

	public String value() default "19";
}


Annotation可以通过枚举类型制定范围 .

示例:

Color类:

public enum Color {
	RED, GREEN, BLUE;
}
MyAnnotation类 :
public @interface MyAnnotation {
	public String key() default "ARES";
	public String value() default "19";
	public Color color() default Color.RED ;
}




4.Rentention和RententionPolicy

在java.lang.annotation中定义了所有与之相关的操作,Rentention本身是一个 Annotation,其中的取值是通过RententionPolicy这个枚举类型制定的.RententionPolicy规定了一下三种范围:

  • 源码中起作用:public static final RetentionPolicy SOURCE

  • 编译后的class中起作用:public static final RetentionPolicy CLASS

  • 运行的时候起作用:public static final RetentionPolicy RUNTIME

如果一个Annotation要起作用,则必须使用RUNTIME范围.


5.反射与Annotation

一个Annotation要想起作用,则必须依靠反射机制,通过反射机制可以取得一个方法在Annotation上声明的全部内容.

取得全部的Annotation.

示例:

import java.lang.annotation.*;
@Retention(value=RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
	public String key() default "ARES";
	public String value();
}

Info类:

package com.ares.demo;
public class Info {
	@Override
	@Deprecated
	@SuppressWarnings(value = "")
	@MyAnnotation(key = "ARES", value = "19")
	public String toString() {
		return "hello";
	}
}
以上三个 Annotation,只有 @Deprecated 是 RUNTIME范围的 .所以运行时 ,只有运行时只有 @Deprecated 可以取到 .

示例:

package com.ares.demo;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class ClassAnnotationDemo {

	public static void main(String[] args) throws Exception {
		Class<?> cls = Class.forName("com.ares.demo.Info");
		Method toStringMethod = cls.getMethod("toString");
		Annotation ans[] = toStringMethod.getAnnotations();// 取得全部的Annotation
		for (int i = 0; i < ans.length; i++) {
			System.out.println(ans[i]) ;
		}
	}

}




6.自定义RUNTIME的Annotation

示例:

package com.ares.demo12;

import java.lang.annotation.*;
@Documented
@Target(value = { ElementType.METHOD, ElementType.TYPE })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
	public String key() default "ARES";

	public String value() default "ARES";
}

通过反射取得Annotation:

package com.ares.demo;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class ClassAnnotationDemo {

	public static void main(String[] args) throws Exception {
		Class<?> cls = Class.forName("com.ares.demo.Info");
		Method toStringMethod = cls.getMethod("toString");
		Annotation ans[] = toStringMethod.getAnnotations();// 取得全部的Annotation
		for (int i = 0; i < ans.length; i++) {
			if (toStringMethod.isAnnotationPresent(MyAnnotation.class)) {
				MyAnnotation my = null; // 声明Annotation的对象
				my = toStringMethod.getAnnotation(MyAnnotation.class) ;
				String key = my.key() ;
				String value = my.value() ;
				System.out.println(key + " --> " + value) ;
			}
		}
	} 
}

实际开发中不用过多关注这些底层的实现,程序中为其提供支持.



7.Annotation深入

①自定义Annotation可以在程序的任意位置使用.

示例:

package com.ares.demo;
@MyAnnotation
public class Info {
	private String name ;
	@MyAnnotation
	public String toString() {
		return "hello" ;
	}
}

注:实际上我们可以为Annotation制定使用范围.

②设置Annotation的使用范围.(实际有8种范围,可以查看手册)

制定范围示例:

package com.ares.demo;

import java.lang.annotation.*;

@Target(value = { ElementType.METHOD, ElementType.TYPE })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
	public String key() default "ARES";

	public String value() default "ARES";
}

③Documented注释

注释格式:

package com.ares.demo;

import java.lang.annotation.*;
@Documented
@Target(value = { ElementType.METHOD, ElementType.TYPE })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
	public String key() default "ARES";

	public String value() default "ARES";
}

在使用类中加入文档注释:

package com.ares.demo;
@MyAnnotation
public class Info {
	private String name ;
	/**
	 * 本方法是覆写Object类中的toString()方法
	 */
	@MyAnnotation
	public String toString() {
		return "hello" ;
	}
}

用文档注释的最大好处是可以导出doc文档.类似官方文档的html格式文档.



8.Inherited

表示此Annotation能否继续被子类继承下去,如果没有写上此注释,表示此Annotation根本就是无法被继承的.

示例:

package com.ares.demo;

import java.lang.annotation.*;
@Inherited
@Documented
@Target(value = { ElementType.METHOD, ElementType.TYPE })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
	public String key() default "ARES";

	public String value() default "ARES";
}
此时表示可以被子类继承 .







20150529


JAVA学习笔记系列

--------------------------------------------

                    联系方式

--------------------------------------------

        Weibo: ARESXIONG

        E-Mail: aresxdy@gmail.com

------------------------------------------------






转载于:https://my.oschina.net/u/2288529/blog/421783

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值