Hero.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package J20250717.demo05;
  2. /**
  3. * @author WanJl
  4. * @version 1.0
  5. * @title Hero
  6. * @description
  7. * @create 2025/7/17
  8. */
  9. public abstract class Hero {
  10. private String name;
  11. private int hp;
  12. private int mp;
  13. private int attack;
  14. private int speed;
  15. private int defenses;
  16. {
  17. //默认进行初始化赋值
  18. this.hp=100;
  19. this.mp=50;
  20. this.attack=15;
  21. this.speed=5;
  22. this.defenses=10;
  23. }
  24. public Hero() {
  25. }
  26. public Hero(String name, int hp, int mp, int attack, int speed, int defenses) {
  27. this.name = name;
  28. this.hp = hp;
  29. this.mp = mp;
  30. this.attack = attack;
  31. this.speed = speed;
  32. this.defenses = defenses;
  33. debut();
  34. }
  35. public String getName() {
  36. return name;
  37. }
  38. public void setName(String name) {
  39. this.name = name;
  40. }
  41. public int getHp() {
  42. return hp;
  43. }
  44. public void setHp(int hp) {
  45. this.hp = hp;
  46. }
  47. public int getMp() {
  48. return mp;
  49. }
  50. public void setMp(int mp) {
  51. this.mp = mp;
  52. }
  53. public int getAttack() {
  54. return attack;
  55. }
  56. public void setAttack(int attack) {
  57. this.attack = attack;
  58. }
  59. public int getSpeed() {
  60. return speed;
  61. }
  62. public void setSpeed(int speed) {
  63. this.speed = speed;
  64. }
  65. public int getDefenses() {
  66. return defenses;
  67. }
  68. public void setDefenses(int defenses) {
  69. this.defenses = defenses;
  70. }
  71. /**
  72. * 普通攻击-平A
  73. * @param h
  74. */
  75. public void a(Hero h){
  76. //你攻击了另一位英雄,
  77. // (你的攻击力-对方的防御力)=对方受到的伤害
  78. //对象的血量-受到的伤害的结果再赋值给对方的血量。
  79. int sh=this.attack-h.getDefenses();
  80. h.setHp(h.getHp()-sh);
  81. System.out.println(this.getName()+"对"+h.getName()+"进行了攻击,造成"+sh+"点伤害");
  82. }
  83. @Override
  84. public String toString() {
  85. return "Hero{" +
  86. "name='" + name + '\'' +
  87. ", hp=" + hp +
  88. ", mp=" + mp +
  89. ", attack=" + attack +
  90. ", speed=" + speed +
  91. ", defenses=" + defenses +
  92. '}';
  93. }
  94. /**
  95. * 英雄登场的方法
  96. */
  97. public abstract void debut();
  98. }