-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path8-classes.ts
58 lines (48 loc) · 1.62 KB
/
8-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
51
52
53
54
55
56
57
58
//A class is a template with code use to create instances with specific data
//A class can contains functions, properties, a contructor abd data
class SmarPhone {
color: string
brand: SmartPhoneBrand
price:number
constructor() {
//default values using constructor
this.color = "white";
this.brand = SmartPhoneBrand.Other;
this.price = 0;
}
//you can initialize and create the properties directly in the constructor
//constructor(public color: string, public brand: SmartPhoneBrand, public price: number) {
// this.color = "white";
// this.brand = SmartPhoneBrand.Other;
// this.price = 0;
//}
setPrice(smartPhonePrice: number) : void
{
this.price = smartPhonePrice;
}
}
//example of a enumeration used in the class
const enum SmartPhoneBrand {
Apple,
Xiaomi,
Sansumg,
Motorola,
Other
}
var mySmartPhone = new SmarPhone();
mySmartPhone.brand = SmartPhoneBrand.Apple;
console.log(`my cellphone is ${mySmartPhone.brand} ${mySmartPhone.color}
nad the price is ${mySmartPhone.price}`);
//You can extends from a class or abstract class
//an Abstract class cannot be implemented
abstract class Card {
//creating and initializing the properties using the constructor
constructor(public cardNumber: number, public owner: string) {}
}
//you need to use super to pass the parameters to the parent class
class CreditCard extends Card {
constructor(public balance: number, cardNumber: number, owner: string) {
super(cardNumber, owner);
}
}
var masterCard = new CreditCard(1000, 11225533645, "Jhon Doe");