C#-方法A中的变量作为方法B的参数
这是我在这里的第一篇帖子,大家好。我刚开始学习C#,在方法上没有什么问题 最重要的是,我必须创建一个程序,它首先向用户询问信息,然后通过另一种方法打印出来。 这只是一段代码:C#-方法A中的变量作为方法B的参数,c#,methods,C#,Methods,这是我在这里的第一篇帖子,大家好。我刚开始学习C#,在方法上没有什么问题 最重要的是,我必须创建一个程序,它首先向用户询问信息,然后通过另一种方法打印出来。 这只是一段代码: namespace Uczymy3 { class Student { public static void GetStudentInformation() { Console.WriteLine("Enter student's first name
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()的结果作为变量放在类Student作用域中,然后在PrintStudentData中作为参数使用吗
我希望一切都解释清楚。最好创建一个Student实体类,让第一个方法返回并传递给第二个方法。像这样
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();
}
}
}
或者我应该把getStudeInformation()的结果作为变量吗
在课堂上使用学生范围,然后在
打印学生数据
这正是你应该做的。
您的代码应如下所示:
var studentInfo = Student.GetStudentInformation();
Student.PrintStudentData(studentInfo.Firstname, studentInfo.BirthDay);
为了使此代码有效,您必须从Student.GetStudentInformation()返回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()返回student的实例:
顺便说一下,在这种情况下,PrintStudentData的参数不应该是“ref”。
“ref”表示传递这些参数,而不是按值传递。使用非静态学生类,并在类主体中定义方法外的字段
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();
}
}
创建属性而不是本地变量我设置“public”以访问这些方法,但感谢您的评论:D@Szkaplerny,公众不是问题,静态是。那么我应该使用“out”选项吗?我在某处读到,“ref”和“out”之间的区别之一是“out”变量必须在方法体中初始化。但在我的示例中,变量并没有在Print的主体中初始化,所以我选择了“ref”。我该怎么做?@Szkaplerny“out”表示通过引用传递变量,但也允许函数更改引用本身()。C#中类的所有实例都会自动通过引用传递,除非它们是原语(字符串、整数、布尔等)。但在您的情况下,没有必要通过引用传递名字和生日,因为您只是在输出它们。
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();
}
}