Last active
August 29, 2015 14:20
-
-
Save markrendle/9551354d2da610790f2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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