[클래스] 상속과 instance of

HeuiEun Choi·2023년 1월 19일
0

javascript

목록 보기
24/39
post-custom-banner

상속


class Vehicle{
	constructor(acceleration = 1){
    	this.speed = 0
      	this.acceleration = 1
    }
  	accelerate(){
    	this.speed += this.acceleration
    }
  	decelerate(){
    	if(this.speed<0){
        	console.log('정지');
          	return 
        }
      this.speed -= this.acceleration
    }
}

class Bicycle extends Vehicle {
	constructor(price = 100, accleration){
    	super(acceleration)
      	this.price = price
      	this.wheel = 2
    }
}

const bicycle = new Bicycle(300, 2);

class Car extends Bicycle{
	constructor(license, price, acceleration){
    	super(price, acceleration)
      	this.license = license
      	this.wheel = 4
    }
  
  	accelerate(){
    	if(!this.license){
        	console.error('무면허')
        }
      this.speed += this.acceleration
      console.log('가속!')
    }
}
const carA = new Car(true, 7000, 10)
const carB = new Car(false, 7000, 10)
profile
당신을 한줄로 소개
post-custom-banner

0개의 댓글