当你的方法需要新参数,但仅在某些情况下......?

发布于 2024-11-05 01:51:36 字数 705 浏览 2 评论 0原文

我有一个方法

myMethod(int a, String b)

,一直运行良好。但是,现在当参数 a 具有特定值时,我需要它执行一些额外的工作。在这种情况下,需要将附加参数传递到争论中。

在所有不需要此参数的情况下,我是否应该将方法签名更改为

myMethod(int a, String b, int newParameter)

并传入空值?

也许保留旧的方法签名,并用空参数调用新方法是最方便的,所以我不必在代码中的其他地方更改任何内容,例如

myMethod(int a, String b) { return myMethod(a, b, null); }

或者我需要以不同的方式看待这个问题吗?

所有评论都非常感谢!


UPDATE

myMethod(int a, String b)

做了很多事情,包括调用

anotherMethod()

当包含 newParameter 时需要发生的唯一区别是 anotherMethod 的调用方式如下

anotherMethod(newParameter)

I have a method

myMethod(int a, String b)

that has been working fine. However, now I need it to perform some additional work when parameter a is of a certain value. In this situation an additional parameter needs to be passed in to the arguements.

Should I just change the method signature to

myMethod(int a, String b, int newParameter)

and pass in nulls in all the cases where this parameter is unnecessary?

Perhaps leaving the old method signature, and getting that to call the new method with a null parameter would be most convenient, so I don't have to change anything elsewhere in the code e.g.

myMethod(int a, String b) { return myMethod(a, b, null); }

Or do I need to look at this differently?

All comments are much appreicated!


UPDATE

myMethod(int a, String b)

does a whole bunch of stuff, including calling

anotherMethod()

the only difference that needs to happen when newParameter is included is that anotherMethod is called like so

anotherMethod(newParameter)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

魂ガ小子 2024-11-12 01:51:41

恕我直言,我认为你走在正确的道路上。我通常保留旧的方法签名并使用附加参数调用该方法。如果你查看公共代码,你会发现同样的技术被大量使用。

IMHO I think you are on the right track. I usually leave the old method signature and call the method with the additional parameters. If you look at commons code you will see the same technique used a lot.

梦里人 2024-11-12 01:51:41

如果调用方法知道何时需要额外参数,那么是的,我倾向于第二种方法。在构造函数中你经常会看到这种情况。或者,如果 myMethod 可以分解为它的组成部分,您可以根据“a”的值调用适当的组成部分。

If the calling method knows when the extra parameter is needed, then yes, I'd lean toward the second approach. You see this all the time with constructors. Alternately, if myMethod can be broken down into it's constituents, you could call the appropriaet constituent part depending on the value of 'a'.

惜醉颜 2024-11-12 01:51:41

如果你的函数需要做两件独立的事情,那么你应该将其设为两个独立的函数,是的。我认为这比拥有一个采用“行为标志”类型参数的函数要清楚得多。

If your function needs to do two separate things, then you should make it two separate functions, yes. I think this is much clearer than having one function which takes a "behaviour flag" kind of argument.

肥爪爪 2024-11-12 01:51:41

就我个人而言,我认为保留旧的方法签名,并使用空参数调用新方法将是更好的选择。我只会编写能够正确处理“null”情况的智能代码。

代码中的冗余越少,维护就越容易。这么想吧。

Personally, I think that leaving the old method signature, and getting that to call the new method with a null parameter would be the better option. I would just write smart code that would handle the case of "null" properly.

The less redundancy in your code, the easier its maintenance will be. Think about it that way.

山人契 2024-11-12 01:51:41

你为什么不这样做呢?

myMethod(int a, String b) {

AnotherMethod();

}

myMethod(int a, String b, int newParameter) {

AnotherMethod(newParameter);

}

替代方案:

myMethod(int a, String b, int newParameter) {

if (newParameter != -1) 
{
AnotherMethod(newParameter);
} else {
AnotherMethod();
}

让 -1 是你永远不会使用的某个值,代表“null”

Why don't you do it like this?

myMethod(int a, String b) {

AnotherMethod();

}

myMethod(int a, String b, int newParameter) {

AnotherMethod(newParameter);

}

Alternative:

myMethod(int a, String b, int newParameter) {

if (newParameter != -1) 
{
AnotherMethod(newParameter);
} else {
AnotherMethod();
}

and let -1 be some value you will never use, representing "null"

孤星 2024-11-12 01:51:41

重载最好这样完成:

/* Some value that is most common to use in your program. */
private final int DEFAULT_PARAM_VALUE = 0;

public void myMethod (int a, String b) {
    myMethod (a, b, DEFAULT_PARAM_VALUE);

public void myMethod (int a, String b, int newParameter) {
    /* do all your main stuff here */

    anotherMethod (newParameter);
}

public void anotherMethod (int param) {
    /* do other stuff */
}

Overloading can be best done like this:

/* Some value that is most common to use in your program. */
private final int DEFAULT_PARAM_VALUE = 0;

public void myMethod (int a, String b) {
    myMethod (a, b, DEFAULT_PARAM_VALUE);

public void myMethod (int a, String b, int newParameter) {
    /* do all your main stuff here */

    anotherMethod (newParameter);
}

public void anotherMethod (int param) {
    /* do other stuff */
}
不打扰别人 2024-11-12 01:51:40

只需重载可以接受附加参数的方法 myMethod 并在需要时调用该特定方法即可。只需调用您需要的方法即可。我认为没有必要从该方法间接调用另一个重载的方法。

myMethod(int a, String b) { return myMethod(a, b, null); }

在这里您可以了解如何调用重载方法。现在只需更改代码本身即可调用必要的方法,否则当程序变大时,程序会变得一团糟。

Just overload the method myMethod that can take the additional parameter and call that specific method, when you need. Just make a call to the method what you need. I don't see any need to make an indirect call from the method to another over loaded method.

myMethod(int a, String b) { return myMethod(a, b, null); }

Here you are any how calling the overloaded method. Just change the code now it self to make the calls to the necessary method, else it will turn to be a mess the program when gets bigger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文