redSkywalker

задача 1 полиморф

Mar 6th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 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 задача_полиморф_1
  8. {
  9.     class Programm
  10.     {
  11.         static void Main()
  12.         {
  13.             IDamageable damageable = new Wolf();
  14.             Kill(damageable);
  15.             Kill(damageable);
  16.  
  17.             Console.WriteLine();
  18.  
  19.             IDamageable damageable1 = new Dragon();
  20.             Kill(damageable1);
  21.             Kill(damageable1);
  22.             Kill(damageable1);
  23.             Kill(damageable1);
  24.             Kill(damageable1);            
  25.  
  26.             Console.ReadKey();
  27.         }
  28.  
  29.         static void Kill(IDamageable damageable)
  30.         {
  31.             damageable.Damage(100);
  32.             if (damageable.IsAlive())
  33.             {
  34.                 Console.WriteLine("Цель ещё жива");
  35.             }
  36.             else
  37.             {
  38.                 Console.WriteLine("Цель мертва");
  39.             }
  40.            
  41.         }
  42.        
  43.     }
  44.  
  45.     interface IDamageable
  46.     {
  47.         void Damage(int damage);
  48.         bool IsAlive();
  49.     }
  50.  
  51.     class Wolf:IDamageable
  52.     {
  53.        public int Health { get; set; }
  54.        public bool isAlive { get; set; }
  55.  
  56.         public Wolf()
  57.         {
  58.             Health = 200;
  59.             isAlive = true;
  60.         }
  61.  
  62.        public bool IsAlive()
  63.         {
  64.             if(this.Health<=0)
  65.             {
  66.                 this.isAlive = false;
  67.             }
  68.  
  69.             return this.isAlive;
  70.         }
  71.  
  72.        public void Damage(int damage)
  73.         {
  74.             this.Health -= damage;
  75.         }
  76.     }
  77.  
  78.     class Dragon : Wolf, IDamageable
  79.     {
  80.         public Dragon() : base()
  81.         {
  82.             Health = 450;
  83.             isAlive = true;
  84.         }
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment