forked from pasuruandev/bahasa-pemrograman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contoh_class.ts
66 lines (59 loc) · 1.76 KB
/
contoh_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
62
63
64
65
66
// Study case : kita akan membuat suatu object user yang berisi id, firstname, lastname. Dan memiliki method login, register
abstract class Root {
abstract done: boolean;
}
class Profile extends Root{
// access modifier : public, private, protected
// properties
id: number;
firstName: string;
lastName: string;
protected save: boolean;
private token: string;
static MAX_FAILED_LOGIN = 2;
private retryLogin = 0;
done: boolean;
// method
login(username: string, password: string) {
this.retryLogin += 1;
if(username == "admin" && password == "admin") {
return true
}
if(this.retryLogin >= Profile.MAX_FAILED_LOGIN) {
return "max login attempted"
}
return false
}
register() { }
constructor(id: number, firstName: string, lastName: string) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.save = false;
this.token = "";
this.done = false;
}
}
Profile.MAX_FAILED_LOGIN = 4; // digunakan untuk menkonfigurasi dari class itu sendiiri
let myProfile = new Profile(1, "Bryan", "tayler");
console.log(myProfile.login("", ""));
console.log(myProfile.login("", ""));
console.log(myProfile.login("", ""));
console.log(myProfile.login("admin", "admin"));
// myProfile
// class inheritance / sub-class
class enhancementProfile extends Profile{
location: string;
constructor(
location: string,
id: number,
firstName: string,
lastName: string
) {
super(id, firstName, lastName);
this.location = location;
this.save;
}
}
let myNewProfile = new enhancementProfile("Surabaya", 2, "margareta", "slavina");