Case: Custom annotations, and use

Case needs:

Define a comment: Book

  • It includes property: String value () Title
  • Properties comprising: double price () price, the default value is 100
  • It contains properties: String [] authors () multiple authors

Custom annotation:

package com.ccc.demo06Annotation;

public @interface Book {
    //书名
    public abstract String value();
    //价格,默认值为 100
    public abstract double price() default 100;
    //多位作者
    public abstract String[] authors();
}

Use custom annotation:

package com.ccc.demo06Annotation;

@Book(value = "梦想与现实",authors = {"小夕","小红"})
public class UseBook {

    @Book(value = "java编程思想",price = 80,authors = "Bruce Eckel")
    private String name;

    @Book(value = "java编程思想",price = 80,authors = "Bruce Eckel")
    public UseBook(String name) {
        this.name = name;
    }

    @Book(value = "java编程思想",price = 80,authors = "Bruce Eckel")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Guess you like

Origin blog.csdn.net/qq_45083975/article/details/91981020