-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.Classes.ts
50 lines (44 loc) · 1.54 KB
/
10.Classes.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
/**
* The `Animal` class represents a generic animal.
*/
class Animal {
// `protected` means the property can be accessed within the class and its subclasses.
// It cannot be accessed outside of these classes.
protected name: string;
/**
* The constructor is a special method that gets called when an instance of the class is created.
* @param name - The name of the animal.
*/
constructor(name: string) {
this.name = name;
}
/**
* This method allows the animal to move.
* @param distance - The distance the animal moves. Default is 0.
*/
public move(distance: number = 0): void {
console.log(`${this.name} moved ${distance}m.`);
}
}
// Create a new instance of the `Animal` class.
let cat = new Animal("Cat");
cat.move(10);
// This will throw an error because the `name` property is protected.
// Protected properties cannot be accessed outside of the class and its subclasses.
// cat.name; // Uncommenting this line will result in an error.
/**
* The `Bird` class is a subclass of `Animal`.
* It inherits properties and methods from the `Animal` class.
*/
class Bird extends Animal {
/**
* This method allows the bird to fly.
* @param distance - The distance the bird flies. Default is 0.
*/
fly(distance: number = 0): void {
console.log(`${this.name} flew ${distance}m.`);
}
}
// Note:
// - `private` properties can only be accessed within the class they are defined.
// - `protected` properties can be accessed within the class and its subclasses.