簡體   English   中英

添加一個屬性來顯示一些信息

[英]Adding a property to display some information

我創建了一個類地址:

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() 方法中提供完整的地址。 此外,還需要在 Student 類中編寫 SetFullAddress() 方法。 我可以從 Main 方法輸入信息(所以其他一切都有效)。 我嘗試了一些不起作用的東西(在 ToString() 方法中顯示完整地址),所以我需要一些幫助。 附言。 如果您以簡單的方式解釋您的答案,我將不勝感激,因為您可能會注意到我是初學者謝謝!

向類型地址的 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。

然后在用戶界面中創建一個新的學生。 請注意,您創建了一個新地址來設置 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM