class Cat {
constructor(name) { //클래스 인스턴스 생성하기(생성자 함수)
this.name = name; //초깃값 설정
}
showName() { //이름을 보여줘 함수
console.log(this.name) //this는Cat class를 의미
}
}
let cat = new Cat('youngei');
cat.showName();
console.log(cat);
class Cat {
constructor(name) { //클래스 인스턴스 생성하기(생성자 함수)
this.name = name; //초깃값 설정
}
showName() { //이름을 보여줘 함수
return this.name; //this는Cat class를 의미
}
}
class MyCat extends Cat { //Cat 클래스 상속 받아오기
constructor(name, age) {
super(name); //super을 매서드로 사용
this.age = age;
}
showName() {
//오버라이딩: 부모 클래스와 같은 이름의 함수 만들기
return '내 고양이 이름은' + super.showName()+'입니다';
}
showAge() {
console.log("내 고양이는" + this.age + '살 입니다!');
}
}
let my_cat = new MyCat('perl', 4);
my_cat.showName();
my_cat.showAge();
차이점 : 오버로딩은 매개변수나 타입이 달라야하지만 오버라이딩은 동일해야하고 오버로딩은 리턴 타입이 무엇이든지 상관없지만 오버라이딩은 동일해야한다는 차이점이 있다.