BasicKnowledgeCSharp/LessonsAndTasks/Lesson 61 - Ключевое слово this, this в конструкторе/Program.cs
Dvurechensky 058c8f2679 1.0
Main
2024-10-05 09:59:53 +03:00

53 lines
1.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
/// <summary>
/// - this - можно получить доступ к текущему экземпляру класса
/// - применяет когда есть не однозначность в именах переменных конструктора и полей класса
/// </summary>
class Student
{
public Student(string lastName, DateTime birthday)
{
this.lastName = lastName;
this.birthday = birthday;
}
public Student(string lastName, string firstName, string middleName, DateTime birthday) : this(lastName, birthday)
{
this.firstName = firstName;
this.middleName = middleName;
}
public Student(Student student)
{
firstName = student.firstName;
lastName = student.lastName;
middleName = student.middleName;
birthday = student.birthday;
}
private string firstName;
private string middleName;
private string lastName;
private DateTime birthday;
public void SetLastName(string lastName) => this.lastName = lastName;
public void Print()
{
Console.WriteLine($"Имя: " + firstName +
$"\nФамилия: " + lastName +
$"\nОтчество: " + middleName +
$"\nДата рождения: " + birthday);
}
}
class Program
{
static void Main()
{
Student student_1 = new Student("Qumo", new DateTime(2000, 10, 5));
student_1.Print();
Console.ReadKey();
}
}