public static void DefBouquet() { Bouquet bouquet1 = new Bouquet(); // вызов одноименных методов bouquet1.Note(); ((IPaper)bouquet1).Note(); bouquet1.Color = "Красный"; bouquet1.Price = 17.4F; bouquet1.SeNumberOfFlowers(ref bouquet1.flowers, 3); Console.WriteLine("--------------------------------------"); Console.WriteLine($"Цвет обертки букета: {bouquet1.Color}\nЦена букета: {bouquet1.Price}\n"); int counter = 1; foreach (var flwr in bouquet1.flowers) { Console.WriteLine($"Цветок {counter++}\nНазвание цветка: {flwr.Name}\nРазмер цветка: {flwr.Size}\n"); } Console.WriteLine("--------------------------------------"); //Bouquet bouquet2 = new Bouquet(); //bouquet2.Color = "Красный"; //bouquet2.Price = 17.4F; //bouquet2.SeNumberOfFlowers(ref bouquet2.flowers, 1); }
/// <summary> /// <para>Работа с объектом через ссылку на интерфейс и абстрактный класс</para> /// <para>Работа is и as</para> /// </summary> public static void ObjViaRefInterface() { IPaper paper = new Bouquet(); paper.Color = "Зеленый"; Console.WriteLine($"\nЦвет: {paper.Color}"); paper.Note(); Plant plant = new Bouquet(); plant.Name = "Ромашка"; Console.WriteLine($"\nНазвание: {plant.Name}"); plant.Note(); Plant plant1 = new Rose(); plant1.Name = "Роза"; plant1.Size = 2.43; Console.WriteLine($"\nНазвание: {plant1.Name}\nРазмер: {plant1.Size}"); plant1.Note(); Console.WriteLine($"{plant1.ToString()}"); Flower flower = new Flower(); Bouquet bouquet = new Bouquet(); int num = 2; //bool checkFlower = flower is Flower; //bool checkBouquet = bouquet is Bouquet; //bool checkNum = num is Flower; bool checkFlower = bouquet is Flower; bool checkBouquet = flower is Bouquet; Console.WriteLine($"\ncheckFlower: {checkBouquet}\ncheckBouquet: {checkFlower}\ncheckNum:"); try { Flower flower1 = new Flower(); Rose rose = flower1 as Rose; Console.WriteLine($"\n{rose.GetType()}"); } catch (NullReferenceException e) { Console.WriteLine(e); } Rose rose1 = new Rose(); Flower flower2 = rose1 as Flower; Console.WriteLine($"\n{flower2.GetType()}"); }