C# - 方法 A 中的变量作为方法 B 的参数
本文关键字:方法 参数 变量 | 更新日期: 2023-09-27 18:36:27
这是我在这里的第一篇文章,所以大家好。我刚开始学习 C#,对方法几乎没有问题。
对于问题的炉膛 - 我必须创建程序,该程序首先向用户询问信息,然后通过另一种方法打印它。它只是一段代码:
namespace Uczymy3
{
class Student
{
public static void GetStudentInformation()
{
Console.WriteLine("Enter student's first name: ");
string firstName = Console.ReadLine();
Console.WriteLine("Enter student's birth date: ");
DateTime birthDay = new DateTime();
birthDay = DateTime.Parse(Console.ReadLine());
}
public static void PrintStudentData(ref string firstName, ref DateTime birthDay)
{
Console.WriteLine("Student {0} was born in {1}", firstName, birthDay);
}
}
class Program
{
static void Main(string[] args)
{
Student.GetStudentInformation();
XXXX;
}
}
}
我的问题始于XXXX。我想做这样的事情:Student.PrintStudentData(Student.GetStudentInformation().firstName, ...)
但我知道这是错误的。那么,如何将一个方法的变量作为另一个方法的参数输入呢?
或者,也许我应该将 GetStudenInformation() 的结果作为变量放在类学生范围中,然后将它们用作 PrintStudentData 中的参数?
我希望一切都解释清楚。
使用非静态学生类并在方法之外的类正文中定义字段。
class Student
{
string firstName;
DateTime birthDay;
public void GetStudentInformation()
{
Console.WriteLine("Enter student's first name: ");
firstName = Console.ReadLine();
Console.WriteLine("Enter student's birth date: ");
birthDay = DateTime.Parse(Console.ReadLine());
}
public void PrintStudentData()
{
Console.WriteLine("Student {0} was born in {1}", firstName, birthDay);
}
}
class Program
{
static void Main(string[] args)
{
var myStudent = new Student();
myStudent.GetStudentInformation();
myStudent.PrintStudentData();
}
}
最好创建一个学生实体类,并使第一个方法返回它并将其传递给第二个方法。喜欢这个。
namespace Uczymy3
{
class Student
{
class StudentInfo{
string firstName {get; set;}
DateTime birthDay {get;set;}
}
public static StudentInfo GetStudentInformation()
{
StudentInfo info = new StudentInfo();
Console.WriteLine("Enter student's first name: ");
info.firstName = Console.ReadLine();
Console.WriteLine("Enter student's birth date: ");
info.birthDay = DateTime.Parse(Console.ReadLine());
return info;
}
public static void PrintStudentData(StudentInfo info)
{
Console.WriteLine("Student {0} was born in {1}", info.firstName, info.birthDay);
}
}
class Program
{
static void Main(string[] args)
{
var info = Student.GetStudentInformation();
PrintStudentData(info);
}
}
}
这不会解决你的问题吗?实例化类 Student
的对象。如果要打印数据,只需在对象上调用方法PrintStudentData
。这是一种更面向对象的方法。
PS:尽量避免不必要的公共静态方法。谷歌这个话题,它会给你很多新的见解。
namespace Uczymy3
{
class Student
{
private readonly string firstName;
private readonly DateTime birthDay;
public Student(string firstName, DateTime birthDay)
{
this.firstName = firstName;
this.birthDay = birthDay;
}
public void PrintStudentData()
{
Console.WriteLine("Student {0} was born in {1}", firstName, birthDay);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter student's first name: ");
string firstName = Console.ReadLine();
Console.WriteLine("Enter student's birth date: ");
DateTime birthDay = DateTime.Parse(Console.ReadLine()); /* not really a user friendly way, but ok :) */
Student student = new Student(firstName, birthDay);
student.PrintStudentData();
}
}
}
或者也许我应该把 GetStudenInformation() 的结果作为变量 在课堂上 学生范围,然后将它们用作参数 打印学生数据?
这正是你应该做的。您的代码应如下所示:
var studentInfo = Student.GetStudentInformation();
Student.PrintStudentData(studentInfo.Firstname, studentInfo.BirthDay);
为了使此代码有效,您必须从 Student.GetStudentInformation() 返回 Student 的实例。
由于您已经有一个 Student 类,因此请向其添加属性并创建一个构造函数来设置这些属性:
class Student {
public string Firstname { get; private set; }
public DateTime BirthDay { get; private set; }
public Student(string firstname, DateTime birthDay) {
Firstname = firstname;
BirthDay = birthDay;
}
...
}
现在你应该能够从 GetStudentInformation() 返回一个学生实例:
public static Student GetStudentInformation()
{
...
return new Student(firstName, birthDay);
}
顺便说一下,在这种情况下,PrintStudentData的参数不应该是"ref"。"ref"表示通过引用而不是按值传递这些参数。