Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring boot中列表的getter和setter_Java_Spring_Spring Boot_Spring Mvc - Fatal编程技术网

Java Spring boot中列表的getter和setter

Java Spring boot中列表的getter和setter,java,spring,spring-boot,spring-mvc,Java,Spring,Spring Boot,Spring Mvc,我正在SpringBoot中创建API。 我认为产品由以下组件组成: 这是我的密码: (实体层) 还是有更好的解决方案?我建议 public List<Component> getProductComponents(){ return this.productComponents; } public void setProductComponents(List<Component> productComponents){ this.productComp

我正在SpringBoot中创建API。 我认为产品由以下组件组成:

这是我的密码: (实体层)


还是有更好的解决方案?

我建议

public List<Component> getProductComponents(){
    return this.productComponents;
}

public void setProductComponents(List<Component> productComponents){
    this.productComponents = productComponents;
}

我建议你

public List<Component> getProductComponents(){
    return this.productComponents;
}

public void setProductComponents(List<Component> productComponents){
    this.productComponents = productComponents;
}

我喜欢第一个。理性存在

  • 我可以通过传递组件列表轻松创建初始对象
  • product.setProductComponents((组件)myProduct.getProductComponents())
    
    getProductComponents
    我假设的井返回列表。这样做是行不通的

    更好的办法是让能手和二传手保持最愚蠢。优点是您可以在将来使用像lombok这样的库省略它们


    您可以随时添加实用方法,我更喜欢第一种。理性存在

  • 我可以通过传递组件列表轻松创建初始对象
  • product.setProductComponents((组件)myProduct.getProductComponents())
    
    getProductComponents
    我假设的井返回列表。这样做是行不通的

    更好的办法是让能手和二传手保持最愚蠢。优点是您可以在将来使用像lombok这样的库省略它们


    您可以随时添加实用方法

    这两种方法都应该有。尽管添加组件的不应称为setter。将其命名为
    addComponent
    或类似的名称。它通常被称为辅助方法,可能会派上用场。两者都应该有。尽管添加组件的不应称为setter。将其命名为
    addComponent
    或类似的名称。它通常被称为辅助方法,可能会派上用场。您现在的问题是什么?在将addProductComponent()方法添加到实体类后,我更新了我的控制器方法,我只想知道我的控制器方法在将组件添加到列表中时是否正确?还是有更好的办法?谢谢。添加许多setProductComponents是正确的方法。对于添加,您应该使用addProductComponentGot。谢谢@Simon您现在的问题是什么?在将addProductComponent()方法添加到实体类后,我已经更新了我的控制器方法,我只想知道我的控制器方法在将组件添加到列表中时是否正确?还是有更好的办法?谢谢。添加许多setProductComponents是正确的方法。对于添加,您应该使用addProductComponentGot。谢谢你,西蒙
    public List<Component> getProductComponents(){
        return this.productComponents;
    }
    
    public void setProductComponents(List<Component> productComponents){
        this.productComponents = productComponents;
    }
    
    public void addProductComponent(Component component) {
       this.productComponents.add(component);
    }
    
    public void removeProductComponent(Component component) {
       this.productComponents.remove(component);
    }