Advertisement
yakovmonarh

Прототип (Prototype)

Aug 21st, 2019
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7.    static void Main(string[] args)
  8.    {
  9.     IFigure rectangle = new Rectangle(10, 10);
  10.     IFigure cloneRectangle = rectangle.Clone();
  11.     Console.WriteLine("Прототип");
  12.     rectangle.GetInfo();
  13.     Console.WriteLine("Клон");
  14.     cloneRectangle.GetInfo();
  15.    
  16.     IFigure circle = new Circle(50);
  17.     IFigure cloneCircle = circle.Clone();
  18.     Console.WriteLine("Круг");
  19.     circle.GetInfo();
  20.     Console.WriteLine("Клон");
  21.     cloneCircle.GetInfo();
  22.    
  23.     Console.ReadLine();
  24.    }
  25. }
  26.  
  27. interface IFigure
  28. {
  29.     IFigure Clone();
  30.     void GetInfo();
  31. }
  32.  
  33. class Rectangle: IFigure
  34. {
  35.     int width;
  36.     int height;
  37.    
  38.     public Rectangle(int w, int h)
  39.     {
  40.         this.width = w;
  41.         this.height = h;
  42.     }
  43.    
  44.     public IFigure Clone()
  45.     {
  46.         return new Rectangle(this.width, this.height);
  47.     }
  48.    
  49.     public void GetInfo()
  50.     {
  51.         Console.WriteLine("Прямоугольник длиной {0} шириной {1}", this.height, this.width);
  52.     }
  53. }
  54.  
  55. class Circle: IFigure
  56. {
  57.     int radius;
  58.    
  59.     public Circle(int r)
  60.     {
  61.         this.radius = r;
  62.     }
  63.    
  64.     public IFigure Clone()
  65.     {
  66.         return new Circle(this.radius);
  67.     }
  68.    
  69.     public void GetInfo()
  70.     {
  71.         Console.WriteLine("Круг радиусом {0}", this.radius);
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement