TS: extends, implements의 차이

Dongwan Kim·2021년 8월 21일
0
class Vehicle {
	honk(): void {
    	console.log('빵빵');
    }
    brake(): void {
    	console.log('브레이크');
    }
    drive(): void {
    	console.log('주행');
    }
}

class Car extends Vehicle {}
const myCar = new Car();
  • extends
    자식 클래스는 부모 클래스의 모든 속성과 메서드를 상속합니다. 이들 중 일부를 재정의하고 새로운 것을 구현할 수 있지만, 부모로부터 상속받은 모든 항목들은 이미 포함되어 있습니다. 따라서 불필요하다면 따로 구현없이 부모클래스의 속성과 메서드를 사용할 수 있습니다.
myCar.drive(); // logs 주행

class Vehicle {
	honk: string;
	brake: boolean;
	drive(): void {}
    
}
=
  • implements
    implements 키워드를 사용하는 클래스(Car)는 구현하려고하는 클래스(Vehicle)의 모든 속성과 메서드를 구현해야 합니다.
class Car implements Vehicle {
	honk: string;
	brake: boolean;
    constructor(honk: string, brake: boolean){
    	this.honk = '빵빵';
        this.brake = false;
    }
	drive(): void {
    		console.log('주행')
        }
}
profile
새로운것에대한 궁금증이 많습니다

0개의 댓글