class, instance 예제 직접 만들어보기

돌리의 하루·2023년 1월 16일
0
  • 조건
    부모 Human //속성 name,age, 메소드 sleep
    sleep는 name이 잠을 잔다는 내용을 리턴한다.

  • 자식 Student이 Human을 상속받는다
    name,age 상속받고 grade 속성 추가
    메소드 study는 num을 인자로 받고 this.grade에 num을 더한다
    name의 성적이 num만큼 올라 ~가 되었다를 리턴

  • new 생성자를 사용하여 Student 객체를 만드는 변수 생성

class Human {
	constructor(name, age) {
    this.name = name;
    this.age = age;
    }
  sleep() {
  return `${this.name}이 잠을 잔다.`
  }
}

class Student extends Human{
	constructor(name, age, grade) {
    this.grade = grade;
    }
  study(num) {
  this.grade = grade + num;
    return `${this.name}의 성적이 ${num}만큼 올라 ${this.grade}이 되었다.`
    
  }
}

let mangoStudent = new Student('mango',18,90)
mangoStudnet.study(10)

study의 grade는 students에서 받아온 속성이라서
study 안에서의 grade를 다른 변수로 설정하는건 안된다.
ex) this.grade를 this.score로 바꾼다 ? => ❌

profile
진화중인 돌리입니다 :>

0개의 댓글