[英]How do I compare Enum values in class to info in main?
這是我上的課:
class student
{
public string Name;
public int Age;
public enum Gender
{
male,
female,
other
}
public void Write()
{
Console.WriteLine("Name: {0}, Age: {1}, ", Name, Age);
}
}
這是主程序:
class Program
{
static void Main(string[] args)
{
student student1 = new student();
student1.Name = "Dan";
student1.Age = 15;
student1.Write();
}
}
當我運行程序時,主程序的名稱和年齡變量將復制到類中的Write
函數中。 我正在嘗試對枚舉值做同樣的事情-我想在主程序中編寫一個性別變量,並向函數添加一些內容也將其寫入,但是我不知道如何對枚舉值進行操作。
如果有人可以幫忙,我很高興聽到您的建議。
在課程中添加性別類型:
public enum Genders { male, female, other }
class student
{
public string Name;
public int Age;
public Genders Gender;
public void Write()
{
Console.WriteLine("Name: {0}, Age: {1}, Gender: {2}", Name, Age, Gender.ToString());
}
}
class Program
{
static void Main(string[] args)
{
student student1 = new student();
student1.Name = "Dan";
student1.Age = 15;
student1.Gender = Genders.male;
student1.Write();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.