fork(1) download
  1. class Player2 {
  2. // メンバ変数
  3. private int hp;
  4. private int place;
  5.  
  6. // 引数なしコンストラクタ
  7. public Player2() {
  8. this.hp = 100;
  9. this.place = 0;
  10. }
  11.  
  12. // hp の getter
  13. public int getHp() {
  14. return this.hp;
  15. }
  16.  
  17. // place の getter
  18. public int getPlace() {
  19. return this.place;
  20. }
  21.  
  22. // work メソッド
  23. public void work() {
  24. this.hp -= 1;
  25. this.place += 1;
  26. }
  27. }
  28.  
  29. // ここを Main に変更しました
  30. public class Main {
  31. public static void main(String[] args) {
  32. // Player2型のインスタンス p を作る
  33. Player2 p = new Player2();
  34.  
  35. // p の中の hp と place を表示(更新前)
  36. System.out.println("hp: " + p.getHp() + ", place: " + p.getPlace());
  37.  
  38. // p に対して work() を呼び出して、hp と place を更新
  39. p.work();
  40.  
  41. // p の中の hp と place を表示(更新後)
  42. System.out.println("hp: " + p.getHp() + ", place: " + p.getPlace());
  43. }
  44. }
Success #stdin #stdout 0.12s 19108KB
stdin
Standard input is empty
stdout
hp: 100, place: 0
hp: 99, place: 1