Advertisement
yakovmonarh

Стратегия (Strategy)

Aug 21st, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.94 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Threading;
  4. using System.Linq;
  5.  
  6. class Program
  7. {
  8.    static void Main(string[] args)
  9.    {
  10.     Car auto = new Car(4, "Volvo", new PetrolMove());
  11.     auto.Move();
  12.     auto.Movable = new ElectricMove();
  13.     auto.Move();
  14.    
  15.     Console.ReadLine();
  16.    }
  17. }
  18.  
  19. interface IMovable
  20. {
  21.     void Move();
  22. }
  23.  
  24. class PetrolMove: IMovable
  25. {
  26.     public void Move()
  27.     {
  28.         Console.WriteLine("Передвижение на бензине");
  29.     }
  30. }
  31.  
  32. class ElectricMove: IMovable
  33. {
  34.     public void Move()
  35.     {
  36.         Console.WriteLine("Передвижение на электричестве");
  37.     }
  38. }
  39.  
  40. class Car
  41. {
  42.     protected int passengers {get;set;}
  43.     protected string model {get;set;}
  44.     public IMovable Movable {private get;set;}
  45.    
  46.     public Car(int num, string model, IMovable mov)
  47.     {
  48.         this.passengers = num;
  49.         this.model = model;
  50.         this.Movable = mov;
  51.     }
  52.    
  53.     public void Move()
  54.     {
  55.         Movable.Move();
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement