Advertisement
Guest User

Наследование

a guest
Jun 19th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | None | 0 0
  1. class Enemy
  2.     {
  3.         protected int helth;
  4.  
  5.         public Enemy()
  6.         {
  7.             helth = 100;
  8.         }
  9.  
  10.         public Enemy(int h)
  11.         {
  12.             helth = h;
  13.         }
  14.  
  15.         public void move()
  16.         {
  17.             Console.WriteLine("Объект переместился на 1 шаг");
  18.         }
  19.  
  20.         public override string ToString()
  21.         {
  22.             return "Enemy\n   Helth: " + Convert.ToString(helth);
  23.         }
  24.     }
  25.  
  26.     class Human : Enemy
  27.     {
  28.         protected int damage;
  29.  
  30.         public Human() : base()
  31.         {
  32.             damage = 0;
  33.         }
  34.  
  35.         public Human(int h, int d) : base(h)
  36.         {
  37.             damage = d;
  38.         }
  39.  
  40.         public void attack()
  41.         {
  42.             Console.WriteLine("Произвден удар в {0} единиц", damage);
  43.         }
  44.  
  45.         public override string ToString()
  46.         {
  47.             return "Human\n   Helth: " + Convert.ToString(helth) +
  48.                         "\n   Damage: " + Convert.ToString(damage);
  49.         }
  50.     }
  51.  
  52.  
  53.     class MainClass
  54.     {
  55.         public static void Main(string[] args)
  56.         {
  57.             Enemy enm = new Enemy(150);
  58.             Human hm = new Human(200, 50);
  59.  
  60.             Console.WriteLine(enm);
  61.  
  62.             enm.move();
  63.             // enm.attack();
  64.  
  65.             Console.WriteLine(hm);
  66.             hm.move();
  67.             hm.attack();
  68.         }
  69.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement