There is no such thing as perfect programming, but we shouldn’t be discouraged because programming is a constant pursuit of perfection.

public class ValidTest {

    public static void main(String[] args) {
        Valid valid = new Valid();

        boolean re = valid.valid(Valid.NAME, Valid.AGE);

        TestPrint.print("valid", re);

        valid.setName("helle");
        valid.setAge("18");

        re = valid.valid(Valid.NAME, Valid.AGE);

        TestPrint.print("valid", re);
    }

    @Data
    static class Valid {
        private String name;
        private String age;

        public static final String NAME = "name";
        public static final String AGE  = "age";

        / / verification
        public boolean valid(String ... fields) {
            Map<String, Boolean> map = setFields(fields);

            if (map.get(NAME) && null! =this.name) {
                return true;
            } else if (map.get(AGE) && null! =this.age) {
                return true;
            }

            return false;
        }

        // Enter all the fields to validate here
        public Map<String, Boolean> setFields(String ... fields) {

            Map<String, Boolean> map = new HashMap<>();

            / / initialization
            map.put(NAME, false);
            map.put(AGE, false);

            // Specify the field to validate, prerequisite - the field to validate is included in the initialization field
            if (null! = fields) {for (String field : fields) {
                    map.put(field, true); }}returnmap; }}} # run result valid ----false
valid ---- true
Copy the code