是否重载方法?

发布于 2024-10-17 08:01:02 字数 1175 浏览 3 评论 0原文

我这里有两种方法(killZombie),用于处理有一个参数(字符串)或多个参数(字符串[])的情况。因为它们做同样的事情,所以我创建了另一个名为“killAZombie”的方法,由其他两个方法使用。我遇到的问题是方法“killAZombie”的命名......有点奇怪。这是其他人也遇到的问题吗?解决这个问题的最佳方法是什么,并将我的“KillAZombie”方法命名为其他与“killZombie”更清楚地区分开来的

public void killZombie(string zombieLocation){
    killAZombie(zombieLocation);
}

public void killZombie(string[] zombieLocations){
    foreach(string zombieLocation in zombieLocations){
        killAZombie(zombieLocation);
    }
}

public void killAZombie(string zombieLocation){
    //Kills a zombie at specified location
}

另一种方法,我可以看到这个问题得到解决,而不是重载“killZombie”,有两种不同的方法,如下所示:

public void killZombie(string zombieLocation){
    //Kills a zombie at specified location
}

public void killZombies(string[] zombieLocations){
    foreach(string zombieLocation in zombieLocations){
        killZombie(zombieLocation);
    }
}

这样我们只有两个更容易理解的方法,但方法不会被重载。在我看来,重载方法似乎是一件好事(这只是意味着方法更少,混乱更少),所以我也不确定这个解决方案。我有兴趣听听解决这个问题的最佳方法是什么,谢谢!

附录:

我的方法实际上需要 4 个参数,因此参数将位于末尾。 params 变量是最重要的变量,因此将其作为最后一个参数来使 params 工作似乎有点笨拙。我是否担心将最重要的参数放在最后,是否足够合法,可以将方法分为 KillZombie 和 KillZombies,或者参数仍然是正确的处理方式?

What I have here are two methods (killZombie) that handle cases where you have one argument (string) or more than one argument (string[]). Because they do the same thing, I made another method named "killAZombie" that is used by the other two methods. The problem I'm having is that the method "killAZombie" is named... well kind of strangely. Is this a problem that other people encounter too? What is the best way to solve this and name my "KillAZombie" method something else that distinguishes itself more clearly from "killZombie"

public void killZombie(string zombieLocation){
    killAZombie(zombieLocation);
}

public void killZombie(string[] zombieLocations){
    foreach(string zombieLocation in zombieLocations){
        killAZombie(zombieLocation);
    }
}

public void killAZombie(string zombieLocation){
    //Kills a zombie at specified location
}

Another way I can see this problem being solved is by instead of overloading "killZombie" have two different methods like this:

public void killZombie(string zombieLocation){
    //Kills a zombie at specified location
}

public void killZombies(string[] zombieLocations){
    foreach(string zombieLocation in zombieLocations){
        killZombie(zombieLocation);
    }
}

This way we only have two methods that are easier to understand, but then the method isn't overloaded. In my mind, it seems like a good thing to have overloaded methods (this just means there are fewer methods, less clutter) so I'm not sure about this solution either. I'd be interested in hearing what would be the best way to tackle this problem, thanks!

Addendum:

My method actually takes 4 arguments, so the params will be at the end. The params variable is the most important one, so putting it as the last argument to make the params work seems kind of clunky. Is my concern over putting the most important argument last, legitimate enough to split up the methods into KillZombie and KillZombies or is the params still the right way to do things?

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

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

发布评论

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

评论(6

早乙女 2024-10-24 08:01:02

这里有一些想法。

首先,公共方法的 C# 惯例是将它们大写:“KillZombie”,而不是“killZombie”。

如果您愿意,您可以仅使用一种方法来完成此操作。这是一种获取一个或多个位置的方法。调用者只需提供一个列表:KillZombies(location1, location2, location3);

private void KillOneZombie(string location) { ... }
public void KillZombies(string location, params string[] additionalLocations)
{
    KillOneZombie(location);
    if (additionalLocations == null) return;
    foreach(string additionalLocation in additionalLocations)
        KillOneZombie(additionalLocation);
}

如果您确实想要有两种方法,请考虑让其中一种方法采用 IEnumerable 而不是数组;这样调用者就可以传入一个列表、一个查询、一个数组等等。

您的第二个命名模式更加标准:KillZombie 和 KillZombies。

params 变量是最重要的变量,因此将其作为最后一个参数来使 params 工作似乎有点笨拙。我是否担心将最重要的参数放在最后,是否足够合法,可以将方法分为 KillZombie 和 KillZombies,或者参数仍然是正确的处理方式?

我会考虑一下您期望如何使用该方法。考虑例如:

Console.WriteLine("User: {0} Score: {1}", user[i].Name, scores[i]);

在这里,我们明确期望“params”将用于支持调用者中的可变数量的参数。从来没有人这样做过:

object[] results = new object[] { user[i].Name, scores[i] };
Console.WriteLine("User: {0} Score: {1}", results);

尽管这是完全合法的。如果您期望您的方法将像 Console.WriteLine 一样使用,其中将传入不同数量的参数,但参数数量在编译时已知,那么请使用 params。

如果您期望它将与第二种模式一起使用——某人有一个位置数组——那么就不要使用 params;创建两个方法,KillZombie 和 KillZombies,并让其中之一采用 IEnumerable 字符串。

Here are some ideas.

First, the C# convention for public methods is to capitalize them: "KillZombie", not "killZombie".

You can do this with just one method if you want. Here's a method that takes one or more locations. The caller can just provide a list: KillZombies(location1, location2, location3);

private void KillOneZombie(string location) { ... }
public void KillZombies(string location, params string[] additionalLocations)
{
    KillOneZombie(location);
    if (additionalLocations == null) return;
    foreach(string additionalLocation in additionalLocations)
        KillOneZombie(additionalLocation);
}

If you do want to have two methods, consider having one take an IEnumerable<string> instead of an array; that way the caller can pass in a list, a query, an array, whatever.

Your second naming pattern is more standard: KillZombie and KillZombies.

The params variable is the most important one, so putting it as the last argument to make the params work seems kind of clunky. Is my concern over putting the most important argument last, legitimate enough to split up the methods into KillZombie and KillZombies or is the params still the right way to do things?

I would think about how you expect the method to be used. Consider for example:

Console.WriteLine("User: {0} Score: {1}", user[i].Name, scores[i]);

Here we clearly expect that the "params" will be used to support a variable number of arguments in the caller. No one ever does this:

object[] results = new object[] { user[i].Name, scores[i] };
Console.WriteLine("User: {0} Score: {1}", results);

even though that is perfectly legal. If you expect that your method is going to be used like Console.WriteLine, where a varying number of parameters is going to be passed in but the number of parameters is known at compile time, then use params.

If you expect that it is going to be used with the second pattern -- someone has an array of locations -- then do not use params; make two methods, KillZombie and KillZombies, and have one of them take an IEnumerable of strings.

对你的占有欲 2024-10-24 08:01:02

在这种情况下,您的两个选择中的后一个可能更可取(考虑到函数的命名意味着它在单个“僵尸”上运行。

但是,您可能还想查看params 关键字,这样您就知道您的选项是什么,例如,如果您将函数简单地命名为 Kill (如果在这种情况下这样做有意义),你可以:

public void Kill(params string[] zombieNames)
{
    foreach(string name in zombieNames)
    {

    }
}

而且你可以用多种方式来称呼它:

Kill("Zoey");
Kill("Francis", "Zoey");

string[] survivors = { "Zoey", "Francis", "Bill", "Louis" };

Kill(names);

(当然,假设你的幸存者都变成了僵尸!)

此外,在风格上,C# 代码通常使用 pascal 大小写作为函数名称(KillAZombie 而不是 killAZombie

编辑附录

是的,参数排序——虽然它没有技术相关性——是一个重要的考虑因素。 API 设计,因此如果您要采用“不太重要”的参数,那么您可能不得不放弃 params

话虽如此,我仍会坚持我最初的建议:该函数的名称为(KillZombieKill),我会坚持使用两个版本,只是为了使您的名称与参数一致。我还建议允许用户指定 IEnumerable 而不是数组。这将允许开发人员使用任何实现 IEnumerable(包括字符串数组)的内容来传递名称。

The latter of your two choices is probably preferable in this case (given that the naming of the function implies that it's operating upon a single "zombie".

However, you might also want to look into the params keyword, just so you know what your options are. For instance, if you'd named your function simply Kill (and if it made sense to do so in this context), you could have:

public void Kill(params string[] zombieNames)
{
    foreach(string name in zombieNames)
    {

    }
}

And you could call it a number of ways:

Kill("Zoey");
Kill("Francis", "Zoey");

string[] survivors = { "Zoey", "Francis", "Bill", "Louis" };

Kill(names);

(Assuming, of course, that your survivors had all been turned into zombies!)

Also, stylistically C# code generally uses pascal casing for function names (KillAZombie rather than killAZombie).

Edit for Addendum

Yes, parameter ordering--while it has no technical relevance--is an important consideration in API design, so if you're going to be taking "less important" parameters, then you'll probably have to do without params.

With that said, I'll stand by my original recommendation: as the function is named (KillZombie versus Kill), I would stick with two versions just for the sake of making your name consistent with the parameters. I would also suggest allowing the user to specify IEnumerable<string> instead of an array. That will allow the developer to pass the names using anything that implements IEnumerable<string>, including a string array.

请爱~陌生人 2024-10-24 08:01:02

在这种情况下,我可能会同意你的第二个建议。 KillZombie 杀死一个僵尸; KillZombies 杀死多个僵尸。

另一种选择是使用带有 params 的单一方法 参数:(

KillZombies("foo");           // kill a single zombie
KillZombies("foo", "bar");    // kill multiple zombies

// ...

public void KillZombies(params string[] zombieLocations)
{
    foreach (string zombieLocation in zombieLocations)
    {
        // kills a zombie at specified location
    }
}

另请注意,标准 C# 命名约定是使用带有大写“K”的 KillZombie/KillZombies。)

In this case I'd probably go with your second suggestion. KillZombie kills a single zombie; KillZombies kills multiple zombies.

Another option would be to use a single method with a params argument:

KillZombies("foo");           // kill a single zombie
KillZombies("foo", "bar");    // kill multiple zombies

// ...

public void KillZombies(params string[] zombieLocations)
{
    foreach (string zombieLocation in zombieLocations)
    {
        // kills a zombie at specified location
    }
}

(Note also that standard C# naming convention would be to use KillZombie/KillZombies with an uppercase "K".)

一曲爱恨情仇 2024-10-24 08:01:02

首先,不仅仅是这两种选择。

特别是,您可以使用第一种方法,而无需使用额外的方法。

public void KillZombie(string zombieLocation){
    // Implement zombie killing logic here.
}

public void KillZombie(string[] zombieLocations){
    foreach(string zombieLocation in zombieLocations)
        KillZombie(zombieLocation);
}

但在这种情况下,我建议采用两种不同的方法。当然,他们做类似的事情,但一个会带走僵尸,一个会带走一个僵尸。方法名称应该反映这一点。

类似地,.NET List 类具有类似的方法 AddAddRange

First of all, there are more than just those two alternatives.

In particular, you can use the first method without the extra method.

public void KillZombie(string zombieLocation){
    // Implement zombie killing logic here.
}

public void KillZombie(string[] zombieLocations){
    foreach(string zombieLocation in zombieLocations)
        KillZombie(zombieLocation);
}

But in this case I would recommend having two different methods. Granted, they do similar things but one takes zombies and one takes a single zombie. The method name should reflect this.

Analogously, the .NET List class has similar methods Add and AddRange.

情定在深秋 2024-10-24 08:01:02

您的示例是错误的 - 您还可以使用 params 数组来允许像
杀死僵尸(位置 1,位置 2,位置 3)。参数数组允许不确定数量的参数。

也就是说,这样做通常是为了更方便使用。如果你有 3 个 ovterloads 因为它们都被使用了,那么拥有它们没有什么问题,或者?

查看不同的String.Format 方法。

Your example is sadly wrong - you could also use a params array to allow calls like
KillZombies(location1, location2, location3). Params arrays allow an underterminedn umber of aprameters.

That said, it often is done for easier use. If you have 3 ovterloads beause they are all used, then there is nothing wrong with having them, or?

Look at the differentString.Format methods.

梦里泪两行 2024-10-24 08:01:02

使用这个怎么样:

public void killZombies(string zombieLocation, params string[] zombieLocations){
    killZombie(zombieLocation);
    if(zombieLocations != null) {
        foreach(string zombieLocation in zombieLocations){
            killZombie(zombieLocation);
        }
    }
}

你可以通过一个或多个僵尸。

[编辑] 如评论所述,此更新不允许杀死任何僵尸

what about using this :

public void killZombies(string zombieLocation, params string[] zombieLocations){
    killZombie(zombieLocation);
    if(zombieLocations != null) {
        foreach(string zombieLocation in zombieLocations){
            killZombie(zombieLocation);
        }
    }
}

You can pass either one or several zombies.

[edit] as commented, this update disallow killing no zombie

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