* Shape.ts
export class Shape {
constructor(private _x: number, private _y: number) {
}
public get x(): number {
return this._x;
}
public set x(value: number) {
this._x = value;
}
public get y(): number {
return this._y;
}
public set y(value: number) {
this._y = value;
}
public getInfo(): string {
return `x=${this._x}, y=${this._y}`;
}
}
* Circle.ts
import { Shape } from "./Shape";
export class Circle extends Shape {
constructor(theX: number, theY: number, private _radius: number) {
super(theX, theY);
}
public get radius(): number {
return this._radius;
}
public set radius(value: number) {
this._radius = value;
}
// Override : 메소드 재정의
public getInfo(): string {
return super.getInfo() + `, radius=${this._radius}`;
}
}
* Driver.ts
import { Shape } from "./Shape";
import { Circle } from "./Circle";
let myShape = new Shape(10, 15);
console.log(myShape.getInfo());
let myCircle = new Circle(5, 10, 20);
console.log(myCircle.getInfo());
<<결과>>
x=10, y=15
x=5, y=10, radius=20