/
index.ts
57 lines (44 loc) · 1.18 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
interface Shape {
name: string;
color: string;
area(): number;
}
function calculateArea(shape: Shape): void {
console.log(`Calculating area of ${shape.name}...`);
console.log(`Area: ${shape.area()}`);
}
const circle: Shape = {
name: "Circle",
color: "Red",
area() {
return Math.PI * 2 * 2;
},
};
function calculatePerimeter(shape: Shape): number {
// Implementation
}
class Circle implements Shape {
constructor(public name: string, public color: string, public radius: number) {}
area(): number {
return Math.PI * this.radius * this.radius;
}
}
const myCircle = new Circle("My Circle", "Blue", 3);
interface ThreeDShape extends Shape {
volume(): number;
}
function calculateVolume(shape: ThreeDShape): void {
console.log(`Calculating volume of ${shape.name}...`);
console.log(`Volume: ${shape.volume()}`);
}
class Sphere implements ThreeDShape {
constructor(public name: string, public color: string, public radius: number) {}
area(): number {
return 4 * Math.PI * this.radius ** 2;
}
volume(): number {
return (4 / 3) * Math.PI * this.radius ** 3;
}
}
const mySphere = new Sphere("My Sphere", "Green", 4);
calculateVolume(mySphere);