gpt4 book ai didi

java - 向现有类添加构造函数效果最小

转载 作者:行者123 更新时间:2023-12-02 13:41:29 25 4
gpt4 key购买 nike

我经常面临这个问题。我不知道,我的做法是否不正确。我工作的项目的代码库非常旧,不可能使用构建器模式或静态工厂更改当前的构造函数,因为数十个其他类使用当前的构造函数。

假设它是当前构造函数;

  // Constructor used until now
public MatchingPortfolioUtil( Integer parameterA, Integer parameterB,
Integer parameterC)
{
this.parameterA = parameterA;
this.parameterB = parameterB;
this.parameterC = parameterC;
}

我想添加以下构造函数。

  // The new constructor
public MatchingPortfolioUtil(Integer parameterA)
{
this.parameterA = parameterA;
}

到目前为止,一切看起来都很正常,但我想要的(如果可能的话,有一个好的设计)是我为新模块创建的构造函数应该只使用我的新模块,因为如果其他人这样做,它可能会导致一些异常通过认为这个构造函数是安全的,在另一个模块中使用它。

最佳答案

The class is tightly coupled and it is more than 1000 lines of codes. I need only one method of the class and for this special method, I do not need other parameters.

听起来这个类有很多责任。

我建议将您需要的方法移至一个单独的(新)类,以便您可以单独使用它。

关于java - 向现有类添加构造函数效果最小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42718831/

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