-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClass.ts
61 lines (48 loc) · 1.93 KB
/
Class.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
57
58
59
60
61
// class Person {
// // Public property
// public name: string;
// // Private property
// private age: number;
// // Protected property
// protected address: string;
// // Readonly property
// readonly birthDate: Date;
// // Constructor
// constructor(name: string, age: number, address: string, birthDate: Date) {
// this.name = name;
// this.age = age;
// this.address = address;
// this.birthDate = birthDate;
// }
// // Getter for age
// public getAge(): number {
// return this.age;
// }
// // Setter for age
// public setAge(age: number): void {
// if (age > 0) {
// this.age = age;
// } else {
// console.error("Age must be positive.");
// }
// }
// // Getter for address
// protected getAddress(): string {
// return this.address;
// }
// // Setter for address
// protected setAddress(address: string): void {
// this.address = address;
// }
// }
// // Example usage
// const person = new Person("John Doe", 30, "123 Main St", new Date("1990-01-01"));
// console.log(person.name); // Public property, accessible
// console.log(person.getAge()); // Using getter for private property
// person.setAge(31); // Using setter for private property
// console.log(person.getAge());
// // Accessing protected property or methods directly from instance is not possible
// // console.log(person.address); // Error: Property 'address' is protected and only accessible within class 'Person' and its subclasses.
// // console.log(person.getAddress()); // Error: Property 'getAddress' is protected and only accessible within class 'Person' and its subclasses.
// console.log(person.birthDate); // Readonly property, accessible but cannot be modified
// // person.birthDate = new Date("2000-01-01"); // Error: Cannot assign to 'birthDate' because it is a read-only property.