# 파라미터로 객체 전달 #### CODE <a class='btn btn-default' href='/codes/49237'>Link</a> ``` public class HeroTest { public static void main(String[] args) { // 객체 생성 Hero thor = new Hero("토르", 150); // thor -> {"토르", 150} Hero thanos = new Hero("타노스", 160); // thanos -> {"타노스", 160} // 토르의 펀치 -> 타노스 thor.punch(thanos); /* 2.코드를 추가하여 펀치를 주고 받으세요. */ thanos.punch(thor); thanos.punch(thor); } } class Hero { // 필드 String name; int hp; // 생성자 Hero(String s, int i) { name = s; hp = i; } // 메소드 void punch(Hero enemy) { // 때린 주체 객체 System.out.printf("[%s]의 펀치!! ", name); /* 1. 맞은 객체에 대한 정보를 출력하세요. */ System.out.printf("%s의 HP: %d -> ", enemy.name, enemy.hp); > enemy.hp -= 10; 이것때문에 [토르]의 펀치!! 타노스의 HP: 160 -> 150 [타노스]의 펀치!! 토르의 HP: 150 -> 140 이렇게 10씩 줄어드는건 알겠어요 그런데 문제에서 [타노스]의 펀치는 이 이후에 한번 더 적용되잖아요 그럼 Hero thor = new Hero("토르", 150); 로 객체의 필드가 초기화 되었던 것 에서 Hero thor = new Hero("토르", 140); 으로 객체의 필드 값도 새로 바뀌는건가요? 언제나 강의 잘 듣고있습니다 선생님..♡♥ System.out.printf("%d\n", enemy.hp); } } ``` #### INPUT ``` ``` #### OUPUT ``` [토르]의 펀치!! 타노스의 HP: 160 -> 150 [타노스]의 펀치!! 토르의 HP: 150 -> 140 [타노스]의 펀치!! 토르의 HP: 140 -> 130 ```
네 맞습니다. 대상의 체력이 펀치 결과에 의해, 10씩 감소됩니다. 다음 코드의 주석을 참고해보세요. ``` public class HeroTest { public static void main(String[] args) { Hero thor = new Hero("토르", 150); // thor -> {"토르", 150} Hero thanos = new Hero("타노스", 160); // thanos -> {"타노스", 160} thor.punch(thanos); // thanos -> {"타노스", 150} thanos.punch(thor); // thor -> {"토르", 140} thanos.punch(thor); // thor -> {"토르", 130} } } ```