在Messages类中有一个名为questionWhatisYourName()
的方法,此方法由另一个名为questionSurname()
的方法调用,此方法由类程序的主方法调用。
是否可以将数据从questionWhatisYourName()
返回到questionSurname()
而不在消息类中调用它?也许我应该将它作为questionSurname()
方法的参数传递?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class TextEffects
{
public void drawLine() {
Console.WriteLine("------------------------------------");
}
public void drawLine(String linetype)
{
Console.WriteLine(linetype +
linetype + linetype + linetype + linetype + linetype + linetype + linetype + linetype + linetype
+ linetype + linetype + linetype + linetype + linetype + linetype + linetype + linetype);
}
}
class Messages
{
public void hello()
{
Console.WriteLine("Hello and welcome.");
}
public String questionWhatIsYourName()
{
Console.WriteLine("What is your name?");
String name = Console.ReadLine();
return name;
}
public void questionSurname()
{
String myname = this.questionWhatIsYourName();
Console.WriteLine(myname + " what is your surname?");
}
}
class Program
{
static void Main(string[] args)
{
TextEffects texteffects = new TextEffects();
Messages messages = new Messages();
texteffects.drawLine("+");
messages.hello();
messages.questionSurname();
}
}
}
答案 0 :(得分:4)
是的,将其作为字符串参数传递。
public void questionSurname(string myname)
{
// ...
}
这样称呼:
string name = messages.questionWhatIsYourName();
messages.questionSurname(name);
答案 1 :(得分:0)
只需将Name添加到类Propeties并使用它:
class Messages
{
private string myName;
public void Hello()
{
Console.WriteLine("Hello and welcome.");
}
public void QuestionWhatIsYourName()
{
Console.WriteLine("What is your name?");
myName = Console.ReadLine();
}
public void QuestionSurname()
{
QuestionWhatIsYourName();
Console.WriteLine(myname + " what is your surname?");
}
}
请注意,您的代码不是标准。方法应该从uppper案例开始 并且类消息的名称只是没有描述该类负责的内容。