我在program.cs中有一个方法,我想将其转移到它自己的类中
如何将pigTalk()转移到它自己的类中?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FunctionTest
{
public class Program
{
public static void pigTalk(string sentence)
{
try
{
while (sentence != "exit")
{
string firstLetter;
string afterFirst;
string pigLatinOut = "";
int x;
string vowel = "AEIOUaeiou";
string[] pieces = sentence.Split();
foreach (string piece in pieces)
{
afterFirst = piece.Substring(1);
firstLetter = piece.Substring(0, 1);
x = vowel.IndexOf(firstLetter);
if (x == -1)
{
pigLatinOut = (afterFirst + firstLetter + "ay ");
}
else
{
pigLatinOut = (firstLetter + afterFirst + "way ");
}
Console.Write(pigLatinOut);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static void Main(string[] args)
{
Console.WriteLine("Enter a sentence to convert into PigLatin");
string sentence = "";
sentence = Console.ReadLine();
pigTalk(sentence);
}
}
}
How can I transfer pigTalk() into its own class?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FunctionTest
{
public class Program
{
public static void pigTalk(string sentence)
{
try
{
while (sentence != "exit")
{
string firstLetter;
string afterFirst;
string pigLatinOut = "";
int x;
string vowel = "AEIOUaeiou";
string[] pieces = sentence.Split();
foreach (string piece in pieces)
{
afterFirst = piece.Substring(1);
firstLetter = piece.Substring(0, 1);
x = vowel.IndexOf(firstLetter);
if (x == -1)
{
pigLatinOut = (afterFirst + firstLetter + "ay ");
}
else
{
pigLatinOut = (firstLetter + afterFirst + "way ");
}
Console.Write(pigLatinOut);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static void Main(string[] args)
{
Console.WriteLine("Enter a sentence to convert into PigLatin");
string sentence = "";
sentence = Console.ReadLine();
pigTalk(sentence);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
创建一个新类并将pigTalk移至其中,
在Main方法中将pigTalk的调用更改为此,
Create a new class and move the pigTalk to it,
in the Main method change the calling of pigTalk to this,
其他人似乎都回答了你的问题,所以我在这里进行推测,但可能引起混乱的一件事是,作为主类中包含的操作方法,让现有方法返回 void 是有意义的(因为一切都是在“程序”的上下文中完成的,所以没有额外的信息可以传递),但作为一种单独的方法,返回结果文本可能更有意义,而不是直接从控制台将其写出到控制台辅助方法;这样,调用对象就可以负责了解如何处理结果。
在有限的上下文中,让该方法将结果打印到控制台并保留原样并不一定是错误的,但如果您的方法只是按照广告执行操作并将其留给更高的权限,那么您将拥有更大的灵活性。确定如何处理结果。因此,您可能会考虑将方法从这样的方法更改
为更像这样的方法
,方法的发出者通过执行以下操作将输出发送到控制台(如果这是您想要做的):
此外,这可能更复杂比您正在寻找的(尽管一旦您理解了这个概念,这并不难),但这看起来像是使用扩展方法的一个很好的例子,这种方法掩盖了对实用程序类的需求,就像您在这里可能需要的那样。要执行类似的操作,您只需更改第一个传入参数(在本例中是您唯一的一个)以应用“this”限定符...因此您最终会得到类似这样的结果:
通过这样做,该方法变得可用作为字符串对象本身的扩展方法,所以你可以在主程序中做这样的事情:
而不是必须做这样的事情:
不过,正如我所说,这对于你需要的东西来说可能有点过分了,所以我希望我并没有与那条小兔子踪迹造成混淆。
Everyone else seems to have answered your question, and so I'm sort of speculating here but one thing that might be causing confusion is the fact that as an operational method contained within your main class, it makes sense to have the existing method return void (because everything is done within the context of "Program", so there's no extra information to pass around), but as a separate method, it would probably make more sense to return the resultant text rather writing it out to the console directly from the helper method; That way, the calling object can be responsible for knowing what to do with the results.
In a limited context, it isn't necessarily wrong to have the method print the results to the console and leave it as that, but you have more flexibility if your method simply performs the operation as advertised and leaves it up to a higher authority to determine what to do with the results. So you might consider changing the method from something like this
to something more like this
Where the issuer of the method sends the output to the console (if that's what you want to do) by doing something like this:
Also, this may be more complex than you're looking for (though it's not really hard once you understand the concept), but this looks like a good example for using an extension method, which kind of masks the need for utility classes like the one you would probably need here. To do something like that, you would just change your first incoming argument (in this case your only one) to apply the "this" qualifier...so you would end up with something like this:
by doing that, the method becomes available as an extended method of the string object itself, so you could do soemthing like this in your main program :
instead of having to do something like this:
As I said, though, that may be overkill for what you need, so I hope I haven't caused confusion with that little rabbit trail.
创建一个新的类文件,然后将方法从 program.cs 剪切 (Ctrl+X) 并粘贴 (Ctrl+V) 到新的类文件中。
然后使用 {YourClassName}.pigTalk() 调用该方法
Create a new class file, and then Cut (Ctrl+X) and Paste (Ctrl+V) the method from program.cs to your new class file.
Then call the method with {YourClassName}.pigTalk()
这是家庭作业吗?这是你必须做的。首先,在主文件所在的目录中创建一个名为“Pig.cs”的新类文件。每个类都应该有自己的文件,但如果将类文件放在解决方案中的单独目录中,这些类将位于不同的命名空间中。在学习 C# 时,您必须学习如何处理命名空间,但我们现在将其放在一边。
Pig.cs 应该包含一个名为 Pig 的类,定义如下:
然后,在您的主程序中,您应该有如下内容:
Is this a homework assignment? Here's what you have to do. First, create a new class file named "Pig.cs" in the same directory as your main file. Every class should have its own file, but if you put class files in a separate directory within your solution, those classes will be in a different namespace. You'll have to learn to deal with namespaces as you learn C#, but we'll put that aside for now.
Pig.cs should contain a class called Pig, defined as follows:
Then, in your main program, you should have something like this: