VitalyD

Untitled

Feb 27th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace num1
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             Enemy enem = new Enemy();
  14.             IDamageable damageable = null;
  15.             Kill(enem);
  16.         }
  17.  
  18.         static void Kill(IDamageable damageable)
  19.         {
  20.             damageable.Damage(100);
  21.             if (damageable.IsAlive())
  22.             {
  23.                 Console.WriteLine("Цель ещё жива");
  24.             }
  25.             else
  26.             {
  27.                 Console.WriteLine("Цель мертва");
  28.             }
  29.         }
  30.     }
  31.  
  32.  
  33.     class Enemy : IDamageable
  34.     {
  35.         private int _hp = 100;
  36.  
  37.  
  38.         public void Damage(int damage)
  39.         {
  40.             _hp -= damage;
  41.         }
  42.  
  43.         public bool IsAlive()
  44.         {
  45.             return _hp > 0;
  46.         }
  47.     }
  48.  
  49.  
  50.     interface IDamageable
  51.     {
  52.         void Damage(int damage);
  53.         bool IsAlive();
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment