添加属性以显示一些信息

时间:2019-01-13 19:34:27

标签: 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 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类的Student:

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: ";
    }
}

在Main方法内部(部分代码):

Address address = new Address(" "," "," ");
        Console.Write("Street: ");
        address.Street = Console.ReadLine();

        Console.Write("City: ");
        address.City = Console.ReadLine();

        Console.Write("Country: ");
        address.Country = Console.ReadLine();

        Console.WriteLine();
        Console.WriteLine("Entered information about the student:");
        student.PrintInformation();
        address.DisplayAddress();
        Console.WriteLine(student.ToString());

我被要求在Student类中添加属性Address,这将帮助我在ToString()方法中提供完整的地址。此外,还必须将SetFullAddress()方法写入Student类中。我可以从Main方法输入信息(其他所有方法都可以)。我尝试了一些不起作用的事情(以在ToString()方法中显示完整地址),因此我需要一些帮助。 附言如果您以简单的方式解释您的答案,我将不胜感激,因为您可能会注意到我是一个初学者 谢谢!

1 个答案:

答案 0 :(得分:0)

向“地址”类型的Student类添加一个属性。

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方法。您可以调用此方法,因为StudentAddres的类型为Address。

然后在用户界面中创建一个新的Student。请注意,您将创建一个新的Address来设置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。设置表示设置属性。就像获取和设置一样。

BTW DOB是出生日期。年龄变化,也许明天,也许下周。我们不知道如果您将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;
}