【问题标题】:Use lombok @getter and @setter to simplify method使用 lombok @getter 和 @setter 来简化方法
【发布时间】:2018-01-02 10:31:51
【问题描述】:

这是我原来的课程。

public class HierarchyTreeNode {
    private String label;
    private List<HierarchyTreeNode> children;

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public List<HierarchyTreeNode> getChildren() {
        return children;
    }

    public void addChild(HierarchyTreeNode child) {
        if (children == null) {
            children = new ArrayList<>();
        }
        children.add(child);
    }
}

我想在这里使用 Lombok @getter 和 @setter。 “addChild”应该简化为setter方法,子节点的初始化应该放在构造函数中。我尝试了以下方法,但无法理解如何使其工作:

public class HierarchyTreeNode {
        @Getter
        private String label;
        private List<HierarchyTreeNode> children;

        public HierarchyTreeNode() {
          this.label = label;
          this.children = children;
        }

        public String getLabel() {
            return label;
        }

        public void setLabel(String label) {
            this.label = label;
        }

        public List<HierarchyTreeNode> getChildren() {
            return children;
        }

        @Setter
        public void addChild(HierarchyTreeNode child) {
            if (children == null) {
                children = new ArrayList<>();
            }
            children.add(child);
        }
    }

我的控制器有以下功能:

   if (!existingNodes.containsKey(childName)) {
            // find node or create new node
            HierarchyTreeNode node = existingNodes.get(parentName);
            if (node == null) {
                // new top level node
                node = new HierarchyTreeNode();
                node.setLabel(parentName);
                root.addChild(node);
                existingNodes.put(parentName, node);
            }
            // add child
            HierarchyTreeNode child = new HierarchyTreeNode();
            child.setLabel(childName);
            node.addChild(child);
            existingNodes.put(childName, child);
        }

如何使用 lombok 来简化 addchild?

【问题讨论】:

  • 你不能。 @Setter 属于字段或类级别。对方法没有意义。
  • 我不知道你想做什么。为什么你有一个注释一个mutator?我会强烈建议您完全避免龙目岛,直到您更好地掌握基础知识。 AOP 真的不简单——不管 Lombok 怎么伪装。
  • 如何使用 lombok 简化 addchild/ 整个类以及如何在构造函数中初始化子类?我是使用龙目岛的新手。我的问题是,如何在该类中使用 getter、setter?
  • @BoristheSpider 请查看原始课程。任何方式 Lombok 方法都可以在该类中使用?

标签: java spring-mvc getter-setter lombok


【解决方案1】:

简短的回答,不,Lombok 不能创建添加方法或执行简单的获取和设置之外的任何操作。

Lombok 注释是类级别的,因此对于我认为您正在尝试做的事情,正确使用如下。您将需要在构造函数中初始化 List。

如果您确实想要自定义设置器,那很好。任何实现都将覆盖 lombok 创建的方法。

这是原始课程的简化版本。 label 将为 null,而 children 列表将为空,而不是 null。

@Getter
@Setter
public class HierarchyTreeNode {

    // Lombok will create a set of get/set methods for these...
    private String label;
    private List<HierarchyTreeNode> children;

    public HierarchyTreeNode() {
      this.children = new ArrayList<>();
    }

    // This isn't a setter.  You are adding hence you need to implement.
    public void addChild(HierarchyTreeNode child) {
      this.children.add(child);
    }
}

【讨论】:

    猜你喜欢
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 2017-06-24
    • 2012-08-01
    • 2020-05-19
    • 2011-11-09
    相关资源
    最近更新 更多