class Animal public string Species get private set
Къде е грешката в кода? class Animal { public string Species { get; private set; } public Animal(string species) { Species = species; } } class Lizard : Animal { public Lizard(string species) { Species = species; } } Copyright © 2013 DAVID Holding Company
Къде е грешката в кода? class Person { private string _name; public string Name { get { return _name; } set { } } } class Program { static void Main(string[] args) { Person person = new Person(); Console. Write. Line("Въведете име на човек: "); person. Name = Console. Read. Line(); } } Copyright © 2013 DAVID Holding Company
Какво ще се случи? class Person { public string Name { get; set; } public void Input. From. Console() { Console. Write. Line("Въведете име на човек: "); Name = Console. Read. Line(); } } class Program { static void Main(string[] args) { try { Person[] persons = new Person[5]; for (int i = 0; i < persons. Length; i++) persons[i]. Input. From. Console(); } catch (Index. Out. Of. Range. Exception ex) { Console. Write. Line("Възникна Index. Out. Of. Range. Exception. "); } catch (IOException ex) { Console. Write. Line("Възникна IOException. "); } Console. Read. Line(); } } Copyright © 2013 DAVID Holding Company
Презаписване на членове class Dog { public Dog(string name) : this(name, 4) { } public Dog(string name, int legs. Count) { Name = name; Legs. Count = legs. Count; } public string Name { get; set; } public int Legs. Count { get; set; } public void Bark. At(string someone) { Console. Write. Line("{0} barks at {1}. ", Name, someone); } public void Bark. At(Dog other. Dog) { Console. Write. Line("{0} barks at {1}. ", Name, other. Dog. Name); } } Copyright © 2013 DAVID Holding Company
Презаписване на членове class Dogs { private readonly Dog[] _dogs = new Dog[50]; public Dog this[int index] { get { return _dogs[index]; } set { _dogs[index] = value; } } public Dog this[string name] { get { foreach (Dog dog in _dogs) { if (dog != null && dog. Name == name) return dog; } return null; } } } Copyright © 2013 DAVID Holding Company
Предефиниране на членове class Dog : Animal { public override void Make. Sound() { Console. Write. Line("Woof woof!"); } } class Cat : Animal { public override void Make. Sound() { Console. Write. Line("Meow. "); } } class Fish : Animal { } Copyright © 2013 DAVID Holding Company
Предефиниране на членове class Book { public string Name { get; set; } public string Author { get; set; } public virtual void Read. From. Console() { Console. Write("Input name: "); Name = Console. Read. Line(); Console. Write("Input author: "); Author = Console. Read. Line(); } } class Scientific. Book : Book { public string Scientific. Field { get; set; } public override void Read. From. Console() { base. Read. From. Console(); Console. Write("Input scientific field: "); Scientific. Field = Console. Read. Line(); } } Copyright © 2013 DAVID Holding Company
Предефиниране на членове class Book { public string Name { get; set; } public string Author { get; set; } public override string To. String() { return string. Format("{0} by {1}", Name, Author); } } Copyright © 2013 DAVID Holding Company
Абстрактни класове и членове abstract class Animal { public abstract void Make. Sound(); } class Dog : Animal { public override void Make. Sound() { Console. Write. Line("Woof woof!"); } } class Cat : Animal { public override void Make. Sound() { Console. Write. Line("Meow. "); } } class Fish : Animal { public override void Make. Sound() { throw new Not. Supported. Exception("Fish don't make sounds. "); } } Copyright © 2013 DAVID Holding Company
Интерфейси interface IVocalizing. Object { void Make. Sound(); } interface INamed. Object { string Name { get; } } Copyright © 2013 DAVID Holding Company
Интерфейси abstract class Animal : IVocalizing. Object { public int Age { get; set; } public abstract void Make. Sound(); } class Dog : Animal, IPet { public string Name { get; set; } public override void Make. Sound() { Console. Write. Line("Woof woof!"); } } class Cat : Animal, IPet { public string Name { get; set; } public override void Make. Sound() { Console. Write. Line("Meow. "); } } Copyright © 2013 DAVID Holding Company
Шаблони class Expression<T> { public T Get. Value() { // Операции } } class Binary. Tree. Node<T> { public T Value { get; set; } public Binary. Tree. Node<T> Left { get; set; } public Binary. Tree. Node<T> Right { get; set; } } Copyright © 2013 DAVID Holding Company
Шаблони • Използване на шаблонни типове – Възможно е влагане на шаблонни типове данни Binary. Tree. Node<Expression<bool>> expression. Node = new Binary. Tree. Node<Expression<bool>>(); Copyright © 2013 DAVID Holding Company
Шаблони class Binary. Tree. Node<T> where T : class { public T Value { get; set; } public Binary. Tree. Node<T> Left { get; set; } public Binary. Tree. Node<T> Right { get; set; } } Copyright © 2013 DAVID Holding Company
Вградени колекции • Пространсвото от имена System. Collections. Generic – – Интерфейсът IEnumerable<T> Интерфейсът ICollection<T> Интерфейсът IList<T> Интерфейсът IDictionary<TKey, TValue> Copyright © 2013 DAVID Holding Company
Въпроси? Copyright © 2013 DAVID Holding Company
Благодаря! • Александър Далемски – – sasho@david. bg musashi. bg@gmail. com Skype: musasho https: //www. facebook. com/adalemski • ДАВИД академия – – acad@david. bg http: //acad. david. bg/ @david_academy https: //www. facebook. com/groups/david. academy/ Copyright © 2013 DAVID Holding Company
- Slides: 60