-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_oriented_programming.html
More file actions
82 lines (63 loc) · 1.9 KB
/
object_oriented_programming.html
File metadata and controls
82 lines (63 loc) · 1.9 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!DOCTYPE html>
<html>
<head>
<title>Object Otiented Programming</title>
</head>
<body>
<script type="text/javascript">
//tworzenie klasy, ktora pozwala nam tworzyc obiekty z parametrami i metodami
class Person {
//i counstructor (WAZNE SLOWO) i makeSound to funckje, ale slowo function jest niepotrzebne
constructor(name, age, food) {
this.name = name;
this.age = Number(age);
this.food = food;
}
makeSound() {
console.log(this.name + " says mmmm to " + this.food);
}
}
const grz = new Person("grz", 26, "ramen")
grz.makeSound();
// pt.2
class Animal {
constructor(species, weight, height) {
this.species = String(species);
this.weight = Number(weight);
this.height = Number(height);
}
getInfo() {
console.log(this.species +
" weighs " + this.weight +
" and is " + this.height + " tall."
);
}
speak() {
console.log("I SAY A THING");
}
}
const tiger = new Animal("tiger", 300, 36);
tiger.getInfo();
// klasa Cat jest dzieckiem klasy Animal, czyli dziedziczy po nim metody
class Cat extends Animal {
constructor(species, weight, height) {
super(species, weight, height); //slowo super wywoluje constructor z klasy rodzica
this.color = "White"; //pierwsze this musi byc zawsze dopiero po super. dodajemy do klasy dziecka dodatkowa ceche, ktorej nie ma w klasie rodzica
}
sleep(){ //nowa metoda, ktorej nie ma w klasie rodzica
console.log(this.species + " is sleeping for very long a day");
return this; //dzieki temu mozliwe jest laczenie funkcji w lancuchy
}
speak(){ //metoda, ktora istnieje w klasie rodzica, ale w klasie dziecka zostaje nadpisana
console.log("moew");
return this;
}
}
const paszczak = new Cat("cat", 4, 1);
console.log(paszczak);
paszczak.getInfo();
paszczak.sleep().speak().sleep(); //chaining funtions
tiger.speak();
</script>
</body>
</html>