注解大全
java四种元注解
元注解:注解的注解,即java为注解开发特准备的注解
@Target
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
表示该注解用于什么地方
public enum ElementType {
/** 类,接口(包括注解类型)或enum声明 */
TYPE,
/** 域声明(包括 enum 实例) */
FIELD,
/** 方法声明 */
METHOD,
/** 参数声明 */
PARAMETER,
/** 构造器声明 */
CONSTRUCTOR,
/** 局部变量声明 */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** 包声明 */
PACKAGE,
/**
* 类型参数声明
*
* @since 1.8
*/
TYPE_PARAMETER,
/**
* 类型的使用
*
* @since 1.8
*/
TYPE_USE
}
@Retention
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
RetentionPolicy value();
}
表示该注解可以保存的范围
public enum RetentionPolicy {
/**
* 源代码:即此注解只能保存在源代码中
* 当编译时,会被丢弃
*/
SOURCE,
/**
* class文件:即此注解可以在class文件中保留
* 但会被jvm丢弃
*/
CLASS,
/**
* 运行期:即此注解可以在运行时保留
* 可以通过反、反射获得
*/
RUNTIME
}
@Documented
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}
即拥有这个注解的元素可以被javadoc此类的工具文档化。它代表着此注解会被javadoc工具提取成文档。在doc文档中的内容会因为此注解的信息内容不同而不同。相当与@return,@param 等
@Inherited
@Inherited是一个标识,用来修饰注解,允许子类继承父类中的注解。拥有此注解的元素其子类可以继承父类的注解
- 该注解作用于整个程序运行中(@Retention(RetentionPolicy.RUNTIME);
- 该注解只能修饰注解(@Target({ElementType.ANNOTATION_TYPE})),因此,它是一个元注解
作用:如果一个类用上了@Inherited修饰的注解,那么其子类也会继承这个注解,注意:
1.接口用上个@Inherited修饰的注解,其实现类不会继承这个注解
2.父类的方法用了@Inherited修饰的注解,子类也不会继承这个注解
当用了@Inherited修饰的注解的@Retention是RetentionPolicy.RUNTIME,则增强了继承性,在反射中可以获取得到
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ATable {
public String name() default "";
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface BTable {
public String name() default "";
}
//作为父类
@ATable
public class Super {
private int superx;
public int supery;
public Super() {
}
//私有
private int superX(){
return 0;
}
//公有
public int superY(){
return 0;
}
}
@BTable
public class Sub extends Super {
private int subx;
public int suby;
private Sub() {
}
public Sub(int i) {
}
//私有
private int subX() {
return 0;
}
//公有
public int subY() {
return 0;
}
}
lombok注解
lombok注解为什么强大
项目中经常使用bean,entity等类,绝大部分数据类类中都需要get、set、toString、equals和hashCode方法,尽管IDE工具都会帮我们生成。但自动生成这些代码后,如果bean中的属性一旦有修改、删除或增加时,需要重新生成或删除get/set等方法,给代码维护增加负担。而使用了lombok之后,就不需要编写或生成setter或getter等方法了,而使用lombok注解的方式,在编译生成的字节码文件中就会存在setter/getter等方法,这样就大大减少了代码量,方便了代码的维护。
lombok注解的使用方式
1.安装lombok插件
使用前需要安装lombok插件
IDEA插件安装地址:https://jingyan.baidu.com/article/0a52e3f4e53ca1bf63ed725c.html
Eclipse插件安装地址:https://jingyan.baidu.com/article/b7001fe1aaa0c30e7282dd8a.html
2.插件配置Maven依赖
在使用lombok插件的时候,还要进行添加lombok的Maven依赖,代码示例:
<!--lombok 注解-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
lombok常用的注解
@Setter @Getter
可以为相应的属性自动生成Getter/Setter方法
package com.example.springboot.model;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.Date;
@Setter
@Getter
@ToString
public class User {
private Integer id;
private String name;
private Integer age;
private String sex;
private Date birthday;
}
@NonNull
该注解用在属性或构造器上,Lombok会生成一个非空的声明,可用于校验参数,能帮助避免空指针。
import lombok.NonNull;
public class NonNullExample extends Something {
private String name;
public NonNullExample(@NonNull Person person) {
super("Hello");
this.name = person.getName();
}
}
@Cleanup
该注解能帮助我们自动调用close()方法,很大的简化了代码。
import lombok.Cleanup;
import java.io.*;
public class CleanupExample {
public static void main(String[] args) throws IOException {
@Cleanup InputStream in = new FileInputStream(args[0]);
@Cleanup OutputStream out = new FileOutputStream(args[1]);
byte[] b = new byte[10000];
while (true) {
int r = in.read(b);
if (r == -1) break;
out.write(b, 0, r);
}
}
}
@EqualsAndHashCode
默认情况下,会使用所有非静态(non-static)和非瞬态(non-transient)属性来生成equals和hasCode,也能通过exclude注解来排除一些属性。
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(exclude={"id", "shape"})
public class EqualsAndHashCodeExample {
private transient int transientVar = 10;
private String name;
private double score;
private Shape shape = new Square(5, 10);
private String[] tags;
private int id;
public String getName() {
return this.name;
}
@EqualsAndHashCode(callSuper=true)
public static class Square extends Shape {
private final int width, height;
public Square(int width, int height) {
this.width = width;
this.height = height;
}
}
}
@ToString
在java.lang.Object中有个实例方法toString,这个方法的作用是一个对象的自我描述,在源码中有这样一句注释,It is recommended that all subclasses override this method.即推荐所有的子类重新该方法。因为该方法在Object中的实现是返回字符串——类名和该对象的hashCode用“@”符连接起来的字符串,不具有可读性。所以,需要重写该方法,使得该方法能够清楚地表述自己的每一个成员变量。现在,我们在已经创建的Student类,重写该方法
@Getter
@Setter
public class Student {
private String name;
private int age;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
@ToString的使用很简单,我们只需将它加到Student类上即可
@Getter
@Setter
@ToString
public class Student {
private String name;
private int age;
}
@Data
注解在类上,会为类的所有属性自动生成setter/getter、equals、canEqual、hashCode、toString方法,如为final属性,则不会为该属性生成setter方法。这个注解可是说是非常强大。
lombok的工作原理:
Lombok本质上就是一个实现了“JSR 269 API”的程序。在使用javac的过程中,它产生作用的具体流程如下:
1.javac对源代码进行分析,生成了一棵抽象语法树(AST)
2.运行过程中调用实现了“JSR 269 API”的Lombok程序
3.此时Lombok就对第一步骤得到的AST进行处理,找到@Data注解所在类对应的语法树(AST),然后修改该语法树(AST),增加getter和setter方法定义的相应树节点
4.javac使用修改后的抽象语法树(AST)生成字节码文件,即给class增加新的节点(代码块)
总结
lombok的使用通过注解的方式减少了getter,setter的手动增加减少了代码量,增加开发效率,特别是在改动属性名字时候维护起来也特别容易。但是同时它也减少了代码的可阅读性降低了源码的舒适度。
二、使用步骤
1.引入库
代码如下(示例):
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
2.读入数据
代码如下(示例):
data = pd.read_csv(
'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())
该处使用的url网络请求的数据。
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。