.NET 4.x Работа с классами - C#
Формулировка задачи:
Добавьте два класса Person и Staff - (это сделал, работает).
Создайте два класса Teacher и Developer, производные от Staff. Для класса Teaсher добавьте поле subject, а для класса Developer - поле level, переопределите метод Print для обоих классов. - тут чтото неполучается...как сделать?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Home_Work_7 { public class Person { private string name; public Person(string name) { this.name = name; } virtual public string Name { get { return name; } } virtual public void Print() { Console.WriteLine("Name: {0}", this.name); } public class Staff : Person { private int salary; public Staff(string name, int salary): base(name) { this.salary = salary; } override public string Name { get { return base.Name + "Staff"; } } override public void Print() { Console.WriteLine("Person {0} has salary: ${1}", Name, this.salary); } public class Teacher : Staff { } public class Developer : Staff { } } } public class Program { static void Main(string[] args) { List<Person> people = new List<Person>(); people.Add(new Person("Yura")); people.Add(new Person.Staff("Ira ", 300)); foreach (var p in people) p.Print(); Console.ReadLine(); } } }
Решение задачи: «.NET 4.x Работа с классами»
textual
Листинг программы
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Home_Work_7 { public class Person { private string name; public Person(string name) { this.name = name; } virtual public string Name { get { return name; } } virtual public void Print() { Console.WriteLine("Name: {0}", this.name); } } public class Staff : Person { private int salary; public Staff(string name, int salary) : base(name) { this.salary = salary; } override public string Name { get { return base.Name + "Staff"; } } override public void Print() { Console.WriteLine("Person {0} has salary: ${1}", Name, this.salary); } public class Teacher : Staff { private string subject; public Teacher(string name, string subject, int salary) : base(name, salary) { this.subject = subject; } override public string Name { get { return base.Name + "Teacher"; } } override public void Print() { Console.WriteLine("Teacher - {0}, subject: {1} has salary: ${2}", Name, this.subject, salary); } } public class Developer : Staff { private string level; public Developer(string name, string level, int salary) : base(name, salary) { this.level = level; } override public string Name { get { return base.Name + "Developer"; } } override public void Print() { Console.WriteLine("Developer - {0}, level - {1} has salary: ${2} ", Name, this.level, salary); } } } public class Program { static void Main(string[] args) { List<Person> people = new List<Person>(); people.Add(new Person("Yura ")); people.Add(new Staff(" Ira ", 300)); people.Add(new Staff.Teacher(" Jaroslav Garasym ", "C# ", 500)); people.Add(new Staff.Developer(" Ivan ", " Profi ", 1000)); foreach (var p in people) { p.Print(); } Console.WriteLine("Enter name: "); string str = Console.ReadLine(); foreach (var p in people) { if (p.Name == str) { p.Print(); } } Console.ReadLine(); } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д