네, 부모 클래스와 자식 클래스는 같은 이름의 메서드를 가질 수 있습니다.
예시
class Parent {
constructor(parentValue) {
this.parentValue = parentValue;
}
//Parent class method name which is same as Child Class method name.
showValues() {
console.log("The parent method is called.....");
console.log("the value is="+this.parentValue);
}
}
class Child extends Parent {
constructor(parentValue,childValue){
super(parentValue);
this.childValue = childValue;
}
//Child class method name which is same as Parent Class method name.
showValues() {
console.log("The child method is called.....");
console.log("The value is="+`${this.childValue}`);
}
}
var parentObject = new Parent(100);
parentObject.showValues();
var childObject = new Child(400,500);
childObject.showValues(); 위의 프로그램을 실행하려면 다음 명령을 사용해야 합니다 -
node fileName.js.
여기에서 내 파일 이름은 demo195.js입니다.
출력
이것은 다음과 같은 출력을 생성합니다 -
PS C:\Users\Amit\javascript-code> node demo195.js The parent method is called..... the value is=100 The child method is called..... The value is=500