[Language/TypeScript] μΆμ ν΄λμ€
2023. 7. 27. 14:02γπ€ Language/TypeScript
μ μ: μΈμ€ν΄μ€ν ν μ μλ ν΄λμ€λ‘ μμ ν΄λμ€μκ² μΆμν¨μλ₯Ό λ°λμ λ§λ€λλ‘ κ°μ ν¨.
abstract class Shape {
abstract getArea(): number; // μΆμ ν¨μ μ μ!!!
printArea() {
console.log(`λν λμ΄: ${this.getArea()}`);
}
}
class Circle extends Shape {
radius: number;
constructor(radius: number) {
super();
this.radius = radius;
}
getArea(): number { // μμ λμ΄λ₯Ό ꡬνλ 곡μμ νμ΄ X λ°μ§λ¦ X λ°μ§λ¦
return Math.PI * this.radius * this.radius;
}
}
class Rectangle extends Shape {
width: number;
height: number;
constructor(width: number, height: number) {
super();
this.width = width;
this.height = height;
}
getArea(): number { // μ¬κ°νμ λμ΄λ₯Ό ꡬνλ 곡μμ κ°λ‘ X μΈλ‘
return this.width * this.height;
}
}
const circle = new Circle(5);
circle.printArea();
const rectangle = new Rectangle(4, 6);
rectangle.printArea();
μ¬μ©λͺ©μ
- μμ ν΄λμ€μ λ°λμ ν¬ν¨μκ³ μΆμ λ΄μ©μ abstract funcν΅ν΄ μ§μ
- μμΈν λ΄μ©μ μμ ν΄λμ€μμ ꡬνν λ μ¬μ©
'π€ Language > TypeScript' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Language/TypeScript] κ°μ²΄μ§ν₯μ€κ³μμΉ: SOLID (0) | 2023.07.27 |
---|---|
[Language/TypeScript] μΈν°νμ΄μ€ (0) | 2023.07.27 |
[Language/TypeScripts] μμ (0) | 2023.07.27 |
[Language/Typescript] ν΄λμ€ (0) | 2023.07.27 |
[Language/TypeScript] μ νΈλ¦¬ν° νμ (0) | 2023.07.26 |