C# 添加属性以显示某些信息
我已经创建了一个类地址:C# 添加属性以显示某些信息,c#,properties,C#,Properties,我已经创建了一个类地址: public class Address { public Address(string street, string city, string country) { this.Street = street; this.City = city; this.Country = country; } public string Street { get; set; } public
public class Address
{
public Address(string street, string city, string country)
{
this.Street = street;
this.City = city;
this.Country = country;
}
public string Street { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string SetFullAddress()
{
return ($"Full address: {Street}, {City}, {Country}");
}
public void DisplayAddress()
{
Console.WriteLine($"Street: {Street}");
Console.WriteLine($"City: {City}");
Console.WriteLine($"Country: {Country}");
Console.WriteLine(SetFullAddress());
}
}
以及继承Person类的另一个类学生:
public class Student:Person
{
private string studentNumber;
public string StudentNumber
{
get
{
return studentNumber;
}
set
{
if(string.IsNullOrEmpty(value))
{
Console.WriteLine("You didn't enter student's number.");
}
else
{
studentNumber = value;
}
}
}
private int age;
public int Age
{
get
{
return age;
}
set
{
try
{
age = value;
if (value < 0 || value > 100)
{
Console.WriteLine("The age you entered is not valid!");
}
}
catch (FormatException)
{
Console.WriteLine("You can enter only integer values.");
}
}
}
private List<int> scores = new List<int>();
public void AddScore()
{
Console.WriteLine("How many scores do you want to add?");
int n = int.Parse(Console.ReadLine());
for (int i = 0; i < n; i++)
{
Console.WriteLine("Please enter the scores:");
try
{
int score = int.Parse(Console.ReadLine());
scores.Add(score);
if (score<0||score>100)
{
Console.WriteLine("Please enter a valid score!");
}
}
catch(FormatException)
{
Console.WriteLine("You cannot enter a non-integer character!");
}
while (i==n)
{
break;
}
}
}
public double AverageScore()
{
double sum = 0;
foreach (var el in scores)
{
sum += el;
}
double averageScore = sum / scores.Count;
return averageScore;
}
public string FullName => $"{FirstName} {LastName}";
public void PrintInformation()
{
Console.WriteLine($"Name: {FirstName}");
Console.WriteLine($"Surname: {LastName}");
Console.WriteLine($"Age: {Age} ");
Console.WriteLine($"ID: {StudentNumber} ");
Console.WriteLine($"Full name: {FullName}");
Console.WriteLine($"Average score: {AverageScore()}");
}
public override string ToString()
{
return $"Student {FullName} with ID {StudentNumber}, is {Age} years old and has an average score {AverageScore()}. The student's full address is: ";
}
}
我被要求在Student类中添加一个属性地址,它将帮助我在ToString()方法中提供完整的地址。此外,SetFullAddress()方法需要在学生类中编写。我可以从Main方法中输入信息(这样其他一切都可以正常工作)。我尝试了一些不起作用的方法(在ToString()方法中显示完整地址),因此我需要一些帮助。
另外,如果你能简单地解释你的答案,我将不胜感激,因为你可能会注意到我是个初学者
谢谢大家! 向Address类型的学生类添加属性
public Address StudentAddress { get; set; }
如此调整您的.ToString覆盖
public override string ToString()
{
return $"Student {FullName} with ID {StudentNumber}, is {Age} years old and has an average score {AverageScore()}. The student's full address is: {StudentAddress.GetFullAddress()} ";
}
在StudentAddress上调用GetFullAddress方法。您可以调用此方法,因为StudentAddress的类型为Address
public Address StudentAddress { get; set; }
然后在用户界面中创建一个新学生。请注意,您创建了一个新地址来设置StudentAddress属性。我把这段代码放在一个按钮点击中,因为我碰巧有一个WinForms应用程序,但它对控制台应用程序的工作原理是一样的
private void button1_Click(object sender, EventArgs e)
{
Student s = new Student();
s.Age = 21;
s.FirstName = "George";
s.LastName = "Washington";
s.StudentNumber = "S54";
s.StudentAddress = new Address("Park Street","Houston", "USA" );
MessageBox.Show(s.ToString());
}
我将SetFullAddress的名称更改为GetFullAddress。Set意味着设置属性。就像在《得到和设定》中一样
顺便说一句,出生日期是出生日期。年龄变了,也许明天,也许下周。我们不知道。如果您使用DOB作为日期,则可以根据需要计算年龄
编辑
根据您的评论添加代码
static void Main(string[] args)
{
Student newStudent = AddStudent();
Console.WriteLine(newStudent); //calls .ToString()
Console.ReadKey();
}
private static Student AddStudent()
{
Student s = new Student();
Console.WriteLine("Enter first name");
s.FirstName = Console.ReadLine();
Console.WriteLine("Enter last Name");
s.LastName = Console.ReadLine();
//etc.
Console.WriteLine("Enter Street");
string Street = Console.ReadLine();
Console.WriteLine("Enter City");
string City = Console.ReadLine();
Console.WriteLine("Enter Country");
string Country = Console.ReadLine();
Address a = new Address(Street, City, Country);
s.StudentAddress = a;
return s;
}
我不认为您真的希望在属性设置器中与用户交互。保存“年龄”而不是DOB也是次优的。您永远不会知道保存的值何时正确或过期。在我看来,SetFullAddress方法是用来设置地址而不是显示地址的。如果我正确理解您需要的内容,您可以在Address类中重写ToString()。@DmitryMerkins否,当我运行程序时,需要在Student类中写入ToString()方法。在分别输入street、city和country后,SetFullAddress可以工作,但我需要在ToString()中写入完整地址方法too@EmptyName对我来说这听起来很奇怪,但可以忽略学生中的ToString()。您的回答让它更清楚了一点,但还有其他一些东西。我应该编写一个代码,允许用户从控制台输入地址(街道、城市、国家)。我编辑了我的帖子,所以你可以在那里看到。你知道怎么做到吗?我不确定Main方法中的第一行。