Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine(s);
            Console.WriteLine("Laboratornya 5: ");
            Console.WriteLine(str);
            Plant bush      = new Bush("Bush", "green", "Normal size", 10);
            Plant kaktus    = new Kaktus("Cactus", "yellow", "Average", 15);
            Plant rose      = new Rose("Rosa", "red", "No't bad size", 50);
            Plant gladiolus = new Gladiolus("Gladiolus", "blue", "Pefect size", 30);

            Console.WriteLine("Information about bush");
            bush.GetInfoAboutPlant();
            Console.WriteLine("Information about kaktus");
            kaktus.GetInfoAboutPlant();
            Console.WriteLine("Information about rose");
            rose.GetInfoAboutPlant();
            Console.WriteLine("Information about gladiolus");
            gladiolus.GetInfoAboutPlant();
            Console.WriteLine(str);
            Console.WriteLine("Metods with one name :");
            Latinname roza = rose;

            roza.Metod();
            Color glad = gladiolus;

            glad.Metod();
            Console.WriteLine(str);
            Console.WriteLine("Using in the code is and as");
            if (glad is Color)
            {
                Console.WriteLine("it's a Year type");
            }
            else
            {
                Console.WriteLine("Oh no i'ts are not Year type");
            }
            if (glad is Latinname)
            {
                Console.WriteLine("it's a Latinname type");
            }
            else
            {
                Console.WriteLine("Sorry i'ts are not Latinname type");
            }
            Object someObj = gladiolus;

            glad = someObj as Gladiolus;
            if (glad != null)
            {
                Console.WriteLine("Wow it's good");
            }
            else
            {
                Console.WriteLine("O no i'ts a very bad");
            }
            Console.WriteLine(str);
            Console.WriteLine("Array of plants:");
            object[] ListOfObjescts = new object[] { bush, kaktus, rose, gladiolus };
            foreach (Latinname cap in ListOfObjescts)
            {
                Printer.iAmPrinting(cap);
            }
            Console.WriteLine(str);
            Console.WriteLine(s);
            Console.WriteLine("Laboratornya 6:");
            Console.WriteLine(str);
            MyEnum enume = MyEnum.Hello;

            Console.WriteLine("Implementation of ENUM:");
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine(enume + " ");
                enume++;
            }
            Console.WriteLine(str);
            SomebodyStruct odv = new SomebodyStruct("APHACA", 1);

            Console.WriteLine("Implementation of STRUCT:");
            odv.DisplayNameAndCoast();
            Console.WriteLine(str);
            Posy posy = new Posy();

            posy[0] = rose;
            posy[1] = gladiolus;
            posy[2] = kaktus;
            Console.WriteLine("Posy consist:");
            posy.OutputPosy();
            Console.WriteLine(str);
            posy.Delete(2);
            Console.WriteLine("Posy consist without 1 flower:");
            posy.OutputPosy();
            Console.WriteLine(str);
            posy.Coast(posy);
            Console.WriteLine(str);
            Console.WriteLine("Sorting of posy by price:");
            posy.Sortirovka(posy);
            Console.WriteLine(str);
            posy.ColorofFlower(posy);
            Console.WriteLine(str);
            Console.WriteLine(s);
            Console.WriteLine("Laboratornya 7:");
            Console.WriteLine(str);
            Console.WriteLine("Exception №1");
            try
            {
                Plant kust = new Bush("Bush", "orange", "Normal size", 600);
                if (kust.coast > 500)
                {
                    throw new СoastException();
                }
            }
            catch (СoastException ex)
            {
                Console.WriteLine($"Type of Error: {ex.typeoferror}");
                Console.WriteLine($"Type of exception: {ex.typeofexception}");
                Console.WriteLine($"Message of Error: {ex.message}");
                Console.WriteLine($"Time of triggering of error: {ex.time}");
            }
            finally
            {
                Console.WriteLine("Example of block finally");
            }
            Console.WriteLine(str);
            Console.WriteLine("Exception №2");
            try
            {
                Plant rosa = new Rose("Rosa", "black", "Little size", 150);
                if (rosa.colorofplant.Equals("blue") == false && rosa.colorofplant.Equals("red") == false &&
                    rosa.colorofplant.Equals("orange") == false && rosa.colorofplant.Equals("yellow") == false &&
                    rosa.colorofplant.Equals("white") == false && rosa.colorofplant.Equals("pink") == false)
                {
                    throw new ColorException();
                }
            }
            catch (ColorException ex)
            {
                Console.WriteLine($"Type of Error: {ex.typeoferror}");
                Console.WriteLine($"Type of exception: {ex.typeofexception}");
                Console.WriteLine($"Message of Error: {ex.message}");
                Console.WriteLine($"Time of triggering of error: {ex.time}");
            }
            finally
            {
                Console.WriteLine("Example of block finally");
            }
            Console.WriteLine(str);
            Console.WriteLine("Exception №3");
            try
            {
                Plant kaktusik = new Gladiolus("Kaktus", "green", "A very long description about little size of kaktus", 210);
                if (kaktusik.typeofrazmer.Length > 20)
                {
                    throw new SizeException();
                }
            }
            catch (SizeException ex)
            {
                Console.WriteLine($"Type of Error: {ex.typeoferror}");
                Console.WriteLine($"Type of exception: {ex.typeofexception}");
                Console.WriteLine($"Message of Error: {ex.message}");
                Console.WriteLine($"Time of triggering of error: {ex.time}");
            }
            finally
            {
                Console.WriteLine("Example of block finally");
            }
            Console.WriteLine(str);
            Console.WriteLine("Exception №4");
            try
            {
                Console.WriteLine("Enter x:");
                int x = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter y:");
                int y = int.Parse(Console.ReadLine());
                int c = x / y;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Message of Error: {ex.Message}");
            }
            Console.WriteLine(str);
            Console.WriteLine("Exception №5");
            try
            {
                int[]  arr  = new int[5];
                Random rand = new Random();
                for (int i = 0; i < 6; i++)
                {
                    arr[i] = rand.Next(0, 10);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Message of Error: {ex.Message}");
            }
            Plant gladus = new Gladiolus("Gladiolus", "blue", "Good size", 700);

            gladus.DebugMetod(gladus);
            Console.WriteLine(str);
            Console.WriteLine(s);
        }
Ejemplo n.º 2
0
      static void Main(string[] args)
      {
          ISomeInterface Rose = new Plant(35);

          Rose.Print();
          Kust kust = new Kust(257, 62.5f);

          kust.PrintGrow();
          kust.Print();
          //обращение к интерфейсу
          ((ISomeInterface)kust).Die();
          ((ISomeInterface2)kust).Die();
          //использование этих классов
          Rose      myByket = new Rose("Роза", 9);
          Gladiolus myGlad  = new Gladiolus("Гладиолус", 17);

          myByket.Draw();
          myByket.Numm();
          Console.WriteLine();
          myGlad.Draw();
          myGlad.Numm();
          Console.WriteLine();

          Plant test = new Plant(100);

          Console.WriteLine("////////////////////////////////////////////////////////////////////");
          // Создадим ссылку на интерфейс
          ISomeInterface obj;

          //Используем ссылку на объект test
          obj = test;
          obj.Die();
          obj.Print();
          //Определить, поддерживает ли данный тип тот или иной интерфейс, можно с использованием ключевого слова as.
          //Если объект удается интерпретировать как указанный интерфейс, то возвращается ссылка на интересующий интерфейс, а если нет, то ссылка null.
          //Следовательно, перед продолжением в коде необходимо предусмотреть проверку на null.
          obj = test as ISomeInterface;
          if (obj != null)
          {
              Console.WriteLine("Тип Plant поддерживает интерфейс ISomeInterface :)");
          }
          else
          {
              Console.WriteLine("Не поддерживает :( ");
          }
          //Проверить, был ли реализован нужный интерфейс, можно также с помощью ключевого слова is.
          //Если запрашиваемый объект не совместим с указанным интерфейсом, возвращается значение false, а если совместим, то можно спокойно вызывать члены этого интерфейса
          if (test is ISomeInterface)
          {
              Console.WriteLine("Тип Plant поддерживает интерфейс ISomeInterface :)");
          }
          else
          {
              Console.WriteLine("Не поддерживает :(");
          }
          Console.WriteLine("////////////////////////////////////////////////////////////////////");
          Rose test2 = new Rose("Роза", 13);
          // Создадим ссылку на абстрактный класс
          Byket obj2;

          //Используем ссылку на объект test2
          obj2 = test2;
          obj2.Draw();
          obj2.Numm();
          Console.WriteLine();

          obj2 = test2 as Byket;
          if (obj2 != null)
          {
              Console.WriteLine("Тип Pose поддержвает абстрактный класс Byket");
          }
          else
          {
              Console.WriteLine("Не поддерживает :(");
          }
          if (test2 is Byket)
          {
              Console.WriteLine("Тип Pose поддержвает абстрактный класс Byket");
          }
          else
          {
              Console.WriteLine("Не поддерживает :(");
          }
          Console.WriteLine("////////////////////////////////////////////////////////////////////");
          //возвращается не только имя класса, но и специфические данные для конкретного экземпляра класса
          Plant ob = new Plant(75);

          Console.WriteLine(ob);

          Kust ob2 = new Kust(197, 15.6f);

          Console.WriteLine(ob2);

          Rose ob3 = new Rose("Белая роза", 15);

          Console.WriteLine(ob3);

          Gladiolus ob4 = new Gladiolus("Гладиолус", 21);

          Console.WriteLine(ob4);
          Console.WriteLine("////////////////////////////////////////////////////////////////////");

          Plant          thisPlant = new Plant(205);
          Gladiolus      UR        = new Gladiolus("Гладиолус", 5);
          ISomeInterface obj7;

          obj7 = thisPlant;
          Byket ttt;

          ttt = UR;
          Printer mmm = new Printer();

          mmm.IAmPrinting(obj7, ttt);
          Console.WriteLine("////////////////////////////////////////////////////////////////////");
          Plant fff = new Plant(456);
          Kust  sss = new Kust(57, 45f);

          // Ссылки на разнотипные объекты классов
          ISomeInterface2 inter;
          ISomeInterface  inter2;

          inter  = sss;
          inter2 = fff;

          Byket ttt2;

          ttt2 = UR;

          // Объявляем и инициализируем массив объектов
          object[,] arrByObject = { { sss, ttt }, { fff, ttt2 } };
          // Выведем в консоль каждый член массива
          foreach (object i in arrByObject)
          {
              Console.WriteLine($"{i} ");
          }
          Console.WriteLine();
      }