在类中的全局变量与方法中的局部变量之间的区别。

huangapple go评论65阅读模式
英文:

Difference global variable in class difference variable in method

问题

我在想将变量声明为全局变量并在服务中使用,以及在方法中使用局部变量的好处可能是什么。可能有好的和不好的地方。例如:

原始代码:

@Service
public class OfficeCodeImpl implements OfficeCodeService {

    @Autowired
    private OfficeCodeRepository officeCodeRepository;

    private List<OfficeCodeDto> list = new ArrayList<>();

    private OfficeCodeEntity officeCodeEntity = new OfficeCodeEntity();

    @Override
    public List<OfficeCodeDto> getAll() {
        officeCodeRepository.findAll().forEach(i -> {
            OfficeCodeDto officeCodeDto = new OfficeCodeDto();
            BeanUtils.copyProperties(i, officeCodeDto);
            list.add(officeCodeDto);
        });
        return list;
    }

    @Override
    public void save(OfficeCodeDto officeCodeDto) {
        BeanUtils.copyProperties(officeCodeDto, officeCodeEntity);
        officeCodeRepository.save(officeCodeEntity);
    }
}

你的理解是,当你调用这个类时,你只需要在方法内部声明所有需要的变量。如果将其实现为全局变量,那么无论是否需要这些变量,都会生成新的值。

修改后的代码:

@Service
public class OfficeCodeImpl implements OfficeCodeService {

    @Autowired
    private OfficeCodeRepository officeCodeRepository;

    @Override
    public List<OfficeCodeDto> getAll() {
        List<OfficeCodeDto> list = new ArrayList<>();
        officeCodeRepository.findAll().forEach(i -> {
            OfficeCodeDto officeCodeDto = new OfficeCodeDto();
            BeanUtils.copyProperties(i, officeCodeDto);
            list.add(officeCodeDto);
        });
        return list;
    }

    @Override
    public void save(OfficeCodeDto officeCodeDto) {
        OfficeCodeEntity officeCodeEntity = new OfficeCodeEntity();
        BeanUtils.copyProperties(officeCodeDto, officeCodeEntity);
        officeCodeRepository.save(officeCodeEntity);
    }
}

这两段代码有何不同?

英文:

I was wondering what might be good side of writing need variable as global in service and difference local varible in the method. What might be good or bad? For example

@Service
public class OfficeCodeImpl implements OfficeCodeService {

    @Autowired
    private OfficeCodeRepository officeCodeRepository;

    private List&lt;OfficeCodeDto&gt; list = new ArrayList&lt;&gt;();

    private OfficeCodeEntity officeCodeEntity = new OfficeCodeEntity();

    @Override
    public List&lt;OfficeCodeDto&gt; getAll() {
        officeCodeRepository.findAll().forEach(i -&gt;{
             OfficeCodeDto officeCodeDto = new OfficeCodeDto();
            BeanUtils.copyProperties(i,officeCodeDto);
            list.add(officeCodeDto);
         });
        return list;
    }

    @Override
    public void save(OfficeCodeDto officeCodeDto) {
       BeanUtils.copyProperties(officeCodeDto,officeCodeEntity);
       officeCodeRepository.save(officeCodeEntity);
    }
    


}

For me I understand that when you call this class only you need method will declare all varible that inside of this method. If you implement it as global variable it will generate new value if this all varible you need all not need.

@Service
public class OfficeCodeImpl implements OfficeCodeService {

    @Autowired
    private OfficeCodeRepository officeCodeRepository;




    @Override
    public List&lt;OfficeCodeDto&gt; getAll() {
        List&lt;OfficeCodeDto&gt; list = new ArrayList&lt;&gt;();
        officeCodeRepository.findAll().forEach(i -&gt;{
             OfficeCodeDto officeCodeDto = new OfficeCodeDto();
            BeanUtils.copyProperties(i,officeCodeDto);
            list.add(officeCodeDto);
         });
        return list;
    }

    @Override
    public void save(OfficeCodeDto officeCodeDto) {
        OfficeCodeEntity officeCodeEntity = new OfficeCodeEntity();
       BeanUtils.copyProperties(officeCodeDto,officeCodeEntity);
       officeCodeRepository.save(officeCodeEntity);
    }



}

Difference this two code ?

答案1

得分: 1

使用第二种方法。

在第一种情况下,如果您两次调用getAll,同一个元素可能会被添加到列表中两次,因此您可能会得到错误的结果。

英文:

Use the second approach.

In the first case, if you call getAll twice, the same element might be added into the list twice, so you might get the wrong result.

huangapple
  • 本文由 发表于 2020年9月7日 12:35:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63771422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定