Guest User

Untitled

a guest
Jun 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. import unject.StandardKernel;
  2. import unject.UnjectModule;
  3. import haxe.rtti.Infos;
  4.  
  5. class Demo{
  6. public static function main()
  7. {
  8. var kernel = new StandardKernel([new TestModule()]);
  9. var samurai = kernel.get(Samurai);
  10.  
  11. samurai.attack("the evildoers");
  12. }
  13. }
  14.  
  15. class TestModule extends UnjectModule
  16. {
  17. public override function load()
  18. {
  19. bind(Katana).toSelf().withParameter("sharpness", 100);
  20. bind(Samurai).toSelf();
  21. bind(IWeapon).to(Sword);
  22. }
  23. }
  24.  
  25. class Katana implements Infos
  26. {
  27. public var sharpness : Int;
  28.  
  29. public function new(sharpness : Int)
  30. {
  31. this.sharpness = sharpness;
  32. }
  33. }
  34.  
  35. class Samurai implements Infos
  36. {
  37. var weapon : IWeapon;
  38.  
  39. public function new(weapon : IWeapon)
  40. {
  41. this.weapon = weapon;
  42. }
  43.  
  44. public function attack(target : String)
  45. {
  46. return weapon.hit(target);
  47. }
  48. }
  49.  
  50. class Sword implements IWeapon, implements Infos
  51. {
  52. public function new();
  53.  
  54. public function hit(target : String)
  55. {
  56. return "Chopped " + target + " in half.";
  57. }
  58. }
  59.  
  60. interface IWeapon
  61. {
  62. public function hit(target:String):String;
  63. }
Add Comment
Please, Sign In to add comment