Class and Objects Lesson
unknown
csharp
a year ago
3.1 kB
14
Indexable
//int[] array = new int[] { 1, 2, 3, 4, 5 }; //Animal sam = new Animal("Sam") { Food = "Dry Feed" }; //Console.WriteLine(sam.Name); //Animal bobby = new Animal(Type.Dog, "Bobby", "Brown Dog", 3, "Dry Food"); //Animal[] animals = new Animal[] { bobby, sam }; //foreach (Animal animal in animals) //{ // Console.WriteLine(animal.Type); //} //Person alemkhan = new Person(); //alemkhan.Name = "alemkhan"; //sam.Age = 23; //Console.WriteLine(alemkhan.Name); //Console.WriteLine(alemkhan.Age); Student student1 = new Student(0, "Alem", "Ut1", "alem1@gmail.com", new DateTime(1998, 1, 22)); Student student2 = new Student(1, "Alem2", "Ut2", "alem2@gmail.com", new DateTime(1998, 2, 22)); Student student3 = new Student(2, "Alem3", "Ut3", "alem3@gmail.com", new DateTime(1998, 3, 22)); Student[] students = new Student[] { student1, student2, student3 }; foreach (Student student in students) { student.ShowData(); } class Student { public int Id; public string FirstName; public string LastName; public string Email; public DateTime DateB; public Student(int id, string firstName, string lastName, string email, DateTime dateB) { Id = id; FirstName = firstName; LastName = lastName; Email = email; DateB = dateB; } public void ShowData() { Console.WriteLine("========================================"); Console.WriteLine($"ID: {Id}"); Console.WriteLine($"Имя: {FirstName}"); Console.WriteLine($"Фамилия: {LastName}"); Console.WriteLine($"E-mail: {Email}"); Console.WriteLine($"Дата рождения: {DateB.ToString("dd M yy")}"); Console.WriteLine("========================================"); } } class Animal { public Type Type; public string Name; public string Description; public string Food; public int Age; // Конструктор public Animal() { Console.WriteLine("Обьект класса Animal был создан"); } public Animal(string name) { Name = name; Console.WriteLine("Обьект класса Animal был создан"); } public Animal(string name, string desc) { Name = name; Description = desc; Console.WriteLine("Обьект класса Animal был создан"); } public Animal(string name, string description, string food) { Name = name; Description = description; Food = food; } public Animal(Type type, string name, string description, string food) { Type = type; Name = name; Description = description; Food = food; } public Animal(Type type, string name, string description, int age, string food) { Type = type; Name = name; Description = description; Food = food; Age = age; } public Animal(Type type, string name, string description, string food, int age) { Type = type; Name = name; Description = description; Food = food; Age = age; } } enum Type { Cat, Dog } class Person { public string Name = "Неизвестно"; public int Age = -1; }
Editor is loading...
Leave a Comment