[small theater] Java notes: kill Yan Liang, punish literary clowns, and sell them at auction

Guan raised his eyes and said to Cao, "I see Yan Liang as if he had put in the first ear of the auction!"

Java annotation is not a program, but an identification in a java program. Java programs can use this identification to make some judgments and execute different calculations and processes.

In the romance of the Three Kingdoms, Guan Gong said that Yan Liang was the first seller. This so-called standard actually has the same meaning as the annotation in Java. It is to mark something on the head of people or code.

Now, let's use the function of Java annotation to simulate the process of Guan Gong's killing Yan Liang and killing Wen Chou.

people

First, create a Person class, which represents everyone. The instance object of this class may be Guan Gong, Yan Liang Wenchou, or just an ordinary soldier.

public class Person {

    /**
     * Is he dead
     */
    private boolean die = false;

    /**
     * Death
     *
     * @return Death
     */
    public boolean isDie() {
        return this.die;
    }

    /**
     * Be killed
     */
    public void beKilled() {
        this.die = true;
    }

    /**
     * killing
     *
     * @param person The murdered man
     */
    public void killed(Person... persons) {
        for (Person person : persons) {
            if (person != null) {
                person.beKilled();
            }
        }
    }
    
}

Mark (note)

Secondly, you should create a Java annotation, which is what Guan Gong calls a tag. You can add a name attribute to this tag, so that you can find the corresponding role through this tag.

/**
 * <p>- @Target(ElementType.FIELD) This annotation is only allowed on class attributes</p>
 * <p>- @Retention(RetentionPolicy.RUNTIME) The annotation is retained at run time</p>
 * <p>- @Inherited The annotation is inherited by subclasses</p>
 * <p>- @Documented This class will be displayed in the document generated by javadoc</p>
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Name {
    String value() default "Small soldier";
}

battlefield

Now that everything is ready, you can arrange the battlefield and create a war Java class to represent the battlefield.

/**
 * Guan raised his eyes and said to Cao, "I see Yan Liang as if he had put in the first ear of the auction!"
 */
public class War {
	// Roles in the battlefield
    private Person person1 = new Person();
    private Person person2 = new Person();
    private Person person3 = new Person();
    private Person person4 = new Person();
    private Person person5 = new Person();
    private Person person6 = new Person();

    public static void main(String[] args) throws IllegalAccessException {
        War war = new War();
        war.start();
    }

    /**
     * The war began
     */
    public void start() throws IllegalAccessException {
        Person Guan Yu = findPerson("Guan Yu");
        Person Yan Liang = findPerson("Yan Liang");
        Person Literary clown = findPerson("Literary clown");
        System.out.println();
        checkStatus();
        if (Guan Yu != null) {
            Guan Yu.killed(Yan Liang, Literary clown);
            checkStatus();
            System.out.println("Guan Yu" + (Yan Liang == null ? "" : "Cut Yan Liang") + (Literary clown == null ? "" : "Kill literary clowns"));
        }
    }

    /**
     * The annotation found by name identifies the corresponding role of the name
     *
     * @param name full name
     * @return The annotation identifies the corresponding role of the name
     */
    public Person findPerson(String name) throws IllegalAccessException {
        Field[] fields = War.class.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(Name.class)) {
                Name annotation = field.getAnnotation(Name.class);
                if (annotation.value().equals(name)) {
                    System.out.print(name + "On the battlefield\t");
                    return (Person) field.get(this);
                }
            }
        }
        System.out.print(name + "Not on the battlefield\t");
        return null;
    }

    /**
     * Check the life and death of the character whose name is annotated on the current battlefield
     */
    public void checkStatus() throws IllegalAccessException {
        Field[] fields = War.class.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(Name.class)) {
                Name annotation = field.getAnnotation(Name.class);
                Person person = (Person) field.get(this);
                if (person.isDie()) {
                    System.out.print(annotation.value() + "Dead\t");
                } else {
                    System.out.print(annotation.value() + "Alive\t");
                }
            }
        }
        System.out.println();
    }

}

make war

The war began

When the simulated war starts, directly execute the main method first.

Outcome of the war:

Guan Yu is not on the battlefield	Yan Liang is not on the battlefield	Wen Chou is not on the battlefield	

Yan Liang enters the battlefield

On a Person type attribute of War class, annotate @ Name("Yan Liang") with the name.

@Name("Yan Liang")
private Person person4 = new Person();

Outcome of the war:

Guan Yu is not on the battlefield	Yan Liang is on the battlefield	Wen Chou is not on the battlefield	
Yan Liang is alive	

Guan Yu enters the battlefield

On a Person type attribute of War class, annotate @ Name("Guan Yu") with the name.

@Name("Guan Yu")
private Person person6 = new Person();

Outcome of the war:

Guan Yu is on the battlefield	Yan Liang is on the battlefield	Wen Chou is not on the battlefield	
Yan Liang is alive	Guan Yu is alive	
Yan Liang is dead	Guan Yu is alive	
Guan Yu cuts Yan Liang

Wen Chou enters the battlefield

On a Person type attribute of War class, annotate @ Name("Wen Chou") with name.

@Name("Literary clown")
private Person person5 = new Person();

Outcome of the war:

Guan Yu is on the battlefield	Yan Liang is on the battlefield	Wen Chou is on the battlefield	
Yan Liang is alive	Wen Chou is alive	Guan Yu is alive	
Yan Liang is dead	Wen Chou is dead	Guan Yu is alive	
Guan Yu killed Yan Liang and Wen Chou

The war is over

Guan Gong rode up the mountain, and all the generals congratulated him. The head of the public offering is before the exercise. Cao said, "the general is a real God and man!"

In this Java program, the Java code has never changed, just added some annotations, and the final running result is completely different. This is the function of Java annotation. Although it is not a part of the program code, it is the identification of the code. You can use this identification to make some judgments and execute different calculations and processes when the program is running.

Most Java frameworks now have versions based on Java annotations, and the principle is the same.

In itself, it makes some program judgment and processing on the self-defined annotations of the framework in the framework. The user only needs to identify some annotations on some codes, and the program can process these codes according to the processing logic of the framework.

Spring, MyBatis, Hibernate and other frameworks are all like this.

Keywords: Java

Added by 0riole on Fri, 31 Dec 2021 02:13:17 +0200