Inheritance
Inheritance is when one class is derived from another class, giving it access to the methods and variables of the base class.
For instance you can have a class Zombie
derive from a class Enemy
which contains stats like health
, attackDamage
and movementSpeed
it also contains a method called TakeDamage()
.
This means that the Zombie
class can use all of these variables and methods, whilst also allowing you to write zombie specific functionality such as EatBrains()
which plays a custom attack animation.
private class Enemy
{
public Player player;
public int health = 5;
public int attackDamage = 1;
public float movementSpeed = 0.5f;
public void TakeDamage(int damage)
{
health -= damage;
}
}
//Can use all of the variables and methods in the Enemy class
private class Zombie : Enemy
{
private void EatBrains()
{
DealDamage(player);
//If close enough to player,
//play animation
}
private void DealDamage(Player player)
{
player.TakeDamage(damage)
}
}