Skip to content

Instantly share code, notes, and snippets.

@markrendle
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markrendle/9551354d2da610790f2d to your computer and use it in GitHub Desktop.
Save markrendle/9551354d2da610790f2d to your computer and use it in GitHub Desktop.
using System;
namespace WizardsAndWarriors
{
public class Program
{
public static void Main(string[] args)
{
IPlayer player = new Wizard();
Console.WriteLine(player.Weapon.Damage);
Console.ReadLine();
}
}
public abstract class Weapon
{
public abstract int Damage { get; }
}
public sealed class Sword : Weapon
{
public override int Damage { get { return 10; } }
}
public sealed class Staff: Weapon
{
public override int Damage { get { return 100; } }
}
public interface IPlayer
{
Weapon Weapon { get; }
}
public abstract class Player<TWeapon> : IPlayer where TWeapon : Weapon
{
public abstract TWeapon Weapon { get; set; }
Weapon IPlayer.Weapon { get { return Weapon; } }
}
public sealed class Warrior : Player<Sword>
{
public override Sword Weapon { get; set; } = new Sword();
}
public sealed class Wizard : Player<Staff>
{
public override Staff Weapon { get; set; } = new Staff();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment