gpt4 book ai didi

java - 新关键字的副作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:34:06 26 4
gpt4 key购买 nike

有人说 new 关键字有副作用是什么意思?在 SSE , 给出了答案的评论,但我没有理解评论。

Clearly the factory methods in the question call new, they have side effects. However, that side effect is identical to just calling a public constructor, so they are no more difficult to test than the latter.

问题与使用静态方法构造具有私有(private)构造函数的对象有关。使用 new 关键字有什么副作用?我应该如何创建我的对象?

最佳答案

full comment是:

Clearly the factory methods in the question call new, they have side effects. However, that side effect is identical to just calling a public constructor, so they are no more difficult to test than the latter.

他是说副作用是创建了一个新的对象;但是在静态工厂方法中调用 new 的副作用与直接调用 new 没有区别。

关于java - 新关键字的副作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46888152/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com