[JS100제] 문제12 : 게임 캐릭터 클래스 만들기

youngseo·2022년 2월 15일
0

JS100제

목록 보기
6/25
post-thumbnail

문제12 : 게임 캐릭터 클래스 만들기

다음 소스코드에서 클래스를 작성하여 게임 캐릭터의 능력치와 '파이어볼'이 출력되게 만드시오.
주어진 소스 코드를 수정해선 안됩니다.

**데이터**
<여기에 class를 작성하세요.>

const x = new Wizard(545, 210, 10);
console.log(x.health, x.mana, x.armor);
x.attack();

**출력**
545 210 10
파이어볼

나의 오답

class Wizard {
  constructor(health, mana, armor){
    this.health = health;
    this.mana = mana;
    this.armor = armor;
    this.attack = '파이어볼';
  }
}

정답

const Wizard = class Wizard {
    constructor (health, mana, armor){
        this.health = health;
        this.mana = mana;
        this.armor = armor;
    }
    attack(){
        console.log('파이어볼');
    }
}

class

class는 객체 지향 프로그래밍에서 특정 객체를 생성하기 위해 변수와 메소드를 정의하는 일종의 틀로, 객체를 정의하기 위한 상태(멤버 변수)와 메서드(함수)로 구성이 됩니다.

예시

class User {
  constructor(name) {
    this.name = name;
  }

  sayHello() {
    console.log(this.name)
  }
}

let user = new User("youngseo");
user.sayHello() //yougseo

0개의 댓글