记录自己在狂神说java中的学习情况,文章里有自己学习的理解和扩展,新手难免有理解偏差或者错误,恳请大佬指正。
Java注解
注解:Java.Annotation
注解和注释。注释是给人看的,注解不仅仅能给人看,还给程序看。
注解入门
什么是注解?
- Annotation是从JDK5开始引入的新技术
- Annotation的作用
- 不是程序本身,可以对程序作出解释(这一点和注释(comment)没有什么区别)
- 可以被其他程序(比如:编译器)读取
- Annotation的格式
- 注解是以"@注释名"在代码中存在的,还可以添加一些参数值,例如@SuppressWarnings(value=“unchecked”)
- Annotation应该在哪里使用?
- 可以附加在package、class、method、field等上面,相当于给他们添加了额外的辅助信息,我们可以通过反射机制编程实现对这些元数据的访问
package annotation;
//注解
public class AnnotationUse extends Object{
// 重写的注解
@Override
public String toString(){
return super.toString();
}
}
内置注解
@Override:定义在java.lang.Override中,此注释只适用于修辞方法,表示一个方法声明打算重写超类中的另一个方法声明。
@Deprecated:定义在java.lang.Deprecated中,此注释可以用于修辞方法,属性,类,表示不鼓励程序员使用这样的元素,通常是因为它很危险或者存在更好的选择。
@SuppressWarnings:定义在java.lang.SuppressWarnings中,用来抑制编译时的警告信息。与前两个注释所有不同,你需要添加一个参数才能正确使用,这些参数都是已经定义好了的。我们选择性的使用就好了。
- @SuppressWarnings(“all”)
- @SuppressWarnings(“unchecked”)
- @SuppressWarnings(“value={“unchecked”,“deprecation”}”)
- 等等…
package annotation;
import java.util.ArrayList;
import java.util.List;
//注解
@SuppressWarnings("all")
public class AnnotationUse extends Object {
// 重写的注解
@Override
public String toString() {
return super.toString();
}
// 不推荐程序员使用,但是可以使用,或者存在更好的方法
@Deprecated
public static void test() {
System.out.println("Deprecated");
}
//镇压警告,可以传递参数,而且可以用在类、方法等好多地方
@SuppressWarnings("all")
public void suppuse() {
List list = new ArrayList();
}
public static void main(String[] args) {
test();
}
}
自定义注解,元注解
元注解
元注解的作用就是负责注解其他注解,Java定义了4个标准的meta-annotation类型,他们被用来提供对其他annotation类型作说明。
这些类型和它们所支持的类在java.lang.annotation包中可以找到。(@Target,@Retention,@Documented,@Inherited)
- @Target:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)
- @Retention:表示需要在什么级别保存该注释信息,用于描述注解的生命周期
- (SOURCE<CLASS<RUNTIME)
- @Document:说明该注解将被包含在javadoc中
- @Inherited:说明子类可以继承父类中的该注解
可以进去看到@Target的源码:
public @interface Target {
/**
* Returns an array of the kinds of elements an annotation type
* can be applied to.
* @return an array of the kinds of elements an annotation type
* can be applied to
*/
ElementType[] value();
}
要求我们传入一个ElementType参数,再打开这个ElementType源码可以看到这是一个枚举类型:
public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
TYPE,
/** Field declaration (includes enum constants) */
FIELD,
/** Method declaration */
METHOD,
/** Formal parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE,
/**
* Type parameter declaration
*
* @since 1.8
*/
TYPE_PARAMETER,
/**
* Use of a type
*
* @since 1.8
*/
TYPE_USE
}
再来看看@Retention的源码:
public @interface Retention {
/**
* Returns the retention policy.
* @return the retention policy
*/
RetentionPolicy value();
}
可以看到也是需要一个RetentionPolicy类型的参数,再看RetentionPolicy的源码可以发现也是一个枚举类型:
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
*/
SOURCE,
/**
* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.
*/
CLASS,
/**
* Annotations are to be recorded in the class file by the compiler and
* retained by the VM at run time, so they may be read reflectively.
*
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME
}
下面可以看看这几个注解的使用:
package annotation;
import java.lang.annotation.*;
//测试元注解
@MyAnnotation
public class MetaAnnotationUse {
@MyAnnotation
public void test() {
}
}
// 定义一个注解
// Target表示我们的注解可以用在哪些范围。这个的使用范围是方法或类或接口或者枚举声明
@Target(value = {ElementType.METHOD, ElementType.TYPE})
// Retention表示我们的注解在什么地方才有效
// runtime>class>sources 定义了runtime,在class和sources里也肯定有效。定义了class,那在sources里也有效
@Retention(value = RetentionPolicy.RUNTIME)
// Documented表示是否将我们的注解生成在Javadoc中
@Documented
// Inherited表示子类可以继承父类的注解
@Inherited
@interface MyAnnotation {
}
自定义注解
- 使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口
分析:
- @interface用来声明一个注解,格式:public @interface 注解名{定义内容}
- 其中的每一个方法实际上是声明了一个配置参数
- 方法的名称就是参数的名称
- 返回值类型就是参数的类型(返回值只能是基本类型、Class、String或enum)
- 可以通过default来声明参数的默认值
- 如果只有一个参数成员,一般参数名为value
- 注解元素必须要有值,我们定义注解元素时,经常使用空字符串、0作为默认值