Annotation注解---详解(一)

本文介绍了Java中的注解技术,包括注解的概念、用途及其三种基本类型:@Override、@Deprecated 和 @SuppressWarnings。此外,还详细解释了每种注解的作用及应用场景。

、Annotation概述

1.annotation其实就是代码里的特殊标记,它用于替代配置文件来告诉类如何运行。有了注解技术后,开发人员可以通过注解告诉类如何运行。

2.三个基本的Annotation

@Override:限定重写父类方法,该注解只能由于方法。可以使用此注解,来说明某个子类的方法是重写了父类方法,当父类方法中没有此方法时,将会在编译时报错。

class MyClass2 extends MyClass1{
	@Override
	public void myMethod1(){
		
	}
	
	@Override
	public void myMethod2(){
		//由于父类MyClass1没有此方法,因此在此方法上添加@Override注解将导致编译报错
	}
}
@Deprecated:用于表示某个程序元素(类,方法,属性等)已经过时,即表示将会废弃,被注解的方法将会有个删除“样式".

@Deprecated
//作用于类
class MyClass2 extends MyClass1{
	@Deprecated
	private String name; //作用于属性
	@Override
	public void myMethod1(){
		
	}
	
	@Override
	public void myMethod2(){
		//由于父类MyClass1没有此方法,因此在此方法上添加@Override注解将导致编译报错
	}
	
	@Deprecated
	public void myMethod3(){
		//作用于方法
	}
}
@SuppressWarnings:抑制编译告警

@SuppressWarnings({ "unchecked", "rawtypes" })
	public List myGetLst(){
		return null;
	}

@SuppressWarnings(option)
This can be categorized into two categories:Put this notation right before the code with warning:
@SuppressWarnings("unused") String unused = "never";
Put this notation right before the class declaration
@SuppressWarnings("deprecation")
class DeprecationExample{ ... }I think the warning options in red should be categoried into category 2.
You can simply try this out!

Warning options
-warn:
allDeprecation
allJavadoc
assertIdentifier
boxing
charConcat
conditionAssign
constructorName
dep-ann
deprecation
discouraged
emptyBlock
enumSwitch
fallthrough
fieldHiding
finalBound
finally
forbidden
hiding
incomplete-switch
indirectStatic
intfAnnotation
intfNonInherited
javadoc
localHiding
maskedCatchBlocks
nls
noEffectAssign
null
over-ann
paramAssign
pkgDefaultMethod
raw
semicolon
serial
specialParamHiding
static-access
staticReceiver
suppress
synthetic-access
syntheticAccess
tasks(<task1>|...|<taskN>)
typeHiding
unchecked
unnecessaryElse
unqualified-field-access
unqualifiedField
unused
unusedArgument
unusedImport
unusedLabel
unusedLocal
unusedPrivate
unusedThrown
uselessTypeCheck
varargsCast
warningToken
Set warning level.
e.g. -warn:unusedLocal,deprecation

In red are the default settings.

-warn:none disable all warnings -warn:<warnings separated by ,> enable exactly the listed warnings -warn:+<warnings separated by ,> enable additional warnings -warn:-<warnings separated by ,> disable specific warnings
allDeprecation deprecation even inside deprecated code
allJavadoc invalid or missing javadoc
assertIdentifier occurrence of assert used as identifier
boxing autoboxing conversion
charConcat when a char array is used in a string concatenation without being converted explicitly to a string
conditionAssign possible accidental boolean assignment
constructorName method with constructor name
dep-ann missing @Deprecated annotation
deprecation usage of deprecated type or member outside deprecated code
discouraged use of types matching a discouraged access rule
emptyBlock undocumented empty block
enumSwitch,
incomplete-switch
incomplete enum switch
fallthrough possible fall-through case
fieldHiding field hiding another variable
finalBound type parameter with final bound
finally finally block not completing normally
forbidden use of types matching a forbidden access rule
hiding macro for fieldHiding, localHiding, typeHiding and maskedCatchBlock
indirectStatic indirect reference to static member
intfAnnotation annotation type used as super interface
intfNonInherited interface non-inherited method compatibility
javadoc invalid javadoc
localHiding local variable hiding another variable
maskedCatchBlocks hidden catch block
nls non-nls string literals (lacking of tags //$NON-NLS-<n>)
noEffectAssign assignment with no effect
null missing or redundant null check
over-ann missing @Override annotation
paramAssign assignment to a parameter
pkgDefaultMethod attempt to override package-default method
raw usage a of raw type (instead of a parametrized type)
semicolon unnecessary semicolon or empty statement
serial missing serialVersionUID
specialParamHiding constructor or setter parameter hiding another field
static-access macro for indirectStatic and staticReceiver
staticReceiver if a non static receiver is used to get a static field or call a static method
suppress enable @SuppressWarnings
syntheticAccess,
synthetic-access
when performing synthetic access for innerclass
tasks enable support for tasks tags in source code
typeHiding type parameter hiding another type
unchecked unchecked type operation
unnecessaryElse unnecessary else clause
unqualified-field-access,
unqualifiedField
unqualified reference to field
unused macro for unusedArgument, unusedImport, unusedLabel, unusedLocal, unusedPrivate and unusedThrown
unusedArgument unused method argument
unusedImport unused import reference
unusedLabel unused label
unusedLocal unused local variable
unusedPrivate unused private member declaration
unusedThrown unused declared thrown exception
uselessTypeCheck unnecessary cast/instanceof operation
varargsCast varargs argument need explicit cast
warningToken unhandled warning token in @SuppressWarnings


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值