Разбор кода - C# (180324)
Формулировка задачи:
Всем привет!
Собственно, помогаю подруге с изучением C# (сам занимаюсь Java & C++), скинула код и подробно попросила объяснить, так как я не мастер в объяснениях, быть может кто поможет?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using System.Threading.Tasks;
namespace Rextester
{
public class Student
{
public string nameofst;
public string surnameofst;
public int ageofst;
public Student()
{
nameofst = "Aziza";
surnameofst = "Sakenova";
ageofst = 18;
}
public Student(string a, string b, int c)
{
this.nameofst = a;
this.surnameofst = b;
this.ageofst = c;
}
public void Create()
{
nameofst = "Aziza2";
surnameofst = "Sakenova";
ageofst = 19;
}
public override string ToString()
{
return ageofst + " " + surnameofst + " " + nameofst;
}
}
public class Program
{
static List<Student> list = new List<Student>();
static void Sort()
{
for (int i = 0; i < list.Count - 1; ++i)
{
for (int j = i + 1; j < list.Count; ++j)
{
if (list[i].ageofst > list[j].ageofst)
{
Student t = list[i];
list[i] = list[j];
list[j] = t;
}
}
}
}
public static void Main(string[] args)
{
Student s = new Student();
Student s2 = new Student("Islam", "Adilshin", 18);
Student s3 = new Student();
Student s4 = new Student("Abay", "Tabynay", 18);
list.Add(s);
list.Add(s2);
list.Add(s3);
list.Add(s4);
Sort();
foreach (Student x in list)
{
Console.WriteLine(x.ToString());
}
}
}
}Решение задачи: «Разбор кода»
textual
Листинг программы
using System;
using System.Collections.Generic;
namespace Rextester
{
public class Student
{
public string nameofst; // имя
public string surnameofst; // фамилия
public int ageofst; // возраст
public Student() // Конструктор по умолчанию (при вызове создаёт восемнадцатилетнюю Azizy Sakenovy)
{
nameofst = "Aziza";
surnameofst = "Sakenova";
ageofst = 18;
}
public Student(string a, string b, int c) // Конструктор с параметрами (сами задаём имя, фамилию и возраст студента)
{
this.nameofst = a;
this.surnameofst = b;
this.ageofst = c;
}
public void Create() // Изменяет данные об этому студенте (студент превращается в девятнадцатилетнюю Azizy II Sakenovy)
{
nameofst = "Aziza2";
surnameofst = "Sakenova";
ageofst = 19;
}
public override string ToString() // Преобразование данных о студенте в одну строку формата Возраст Имя Фамилия
{
return ageofst + " " + surnameofst + " " + nameofst;
}
}
public class Program
{
static List<Student> list = new List<Student>(); // список студентов создаём
static void Sort() // сортировка студентов по возрасту методом пузырька
{
for (int i = 0; i < list.Count - 1; ++i)
{
for (int j = i + 1; j < list.Count; ++j)
{
if (list[i].ageofst > list[j].ageofst)
{
Student t = list[i];
list[i] = list[j];
list[j] = t;
}
}
}
}
public static void Main(string[] args)
{
Student s = new Student(); // создали студента с помощью конструктора без параметров (Азиза, 18 лет)
Student s2 = new Student("Islam", "Adilshin", 18); // создали второго студента с помощью параметризированного конструктора
Student s3 = new Student(); // создали ещё одну Азизу
Student s4 = new Student("Abay", "Tabynay", 18); // ещё пацан
list.Add(s); // всех добавили в список
list.Add(s2);
list.Add(s3);
list.Add(s4);
Sort(); // и отсортировали
foreach (Student x in list) // цикл по списку со студентиками
{
Console.WriteLine(x.ToString()); // преобразуем студента в строку и выводим
}
}
}
}