-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19-objeto.js
More file actions
178 lines (155 loc) · 4.2 KB
/
19-objeto.js
File metadata and controls
178 lines (155 loc) · 4.2 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// // Defenindo classe pessoa
// class Pessoa {
// constructor(pNome, PIdade) {
// this.nome = pNome;
// this.idade = PIdade;
// }
// apresentarSe() {
// console.log(`Olá, meu nome é ${this.nome} e eu tenho ${this.idade} anos`);
// }
// envelhecer() {
// this.idade++;
// }
// }
// //Criando objetos do "tipo" pessoa
// var pessoa1 = new Pessoa("Dienifer", 52);
// var p2 = new Pessoa("Taldo pedrin", 18);
// pessoa1.apresentarSe();
// p2.apresentarSe();
// p2.envelhecer();
// p2.apresentarSe();
// pessoa1.envelhecer();
// pessoa1.apresentarSe();
// console.log("----------------------------");
// //------------------------------------------------------------------------------
// //Abstração: Abstrair as caracteristicas mais importantes e descartar detalhes irrelevantes
// class Pet {
// constructor(nome, idade, tipo) {
// this.nome = nome;
// this.idade = idade;
// this.tipo = tipo;
// }
// alimentar() {
// console.log(`${this.nome} foi alimentado!`);
// }
// }
// const meuPet = new Pet("Rex", 2, "Cachorro");
// console.log(
// `Meu pet é um ${meuPet.tipo}, chamado ${meuPet.nome} e tem ${meuPet.idade} anos!`
// );
// meuPet.alimentar();
// console.log("--------------------Herança--------------------");
// class Animal {
// constructor(nome, idade, tipoAlimentacao) {
// this.nome = nome;
// this.idade = idade;
// this.tipoAlimentacao = tipoAlimentacao;
// }
// alimentar() {
// console.log(`O animal ${this.nome} foi alimentado!`);
// }
// emitirSom() {
// console.log(`Barulho padrão de animal!`);
// }
// }
// class Cachorro extends Animal {
// alimentar() {
// console.log(`O cachorro ${this.nome} foi alimentado`);
// }
// emitirSom() {
// console.log(`🐶 Au au! 🐶`);
// }
// }
// const pet = new Animal("Carlos", 10, "Comida espacial");
// const meuCachorro = new Cachorro("Rex", 3, "Ração");
// pet.alimentar();
// pet.emitirSom();
// console.log(" ");
// meuCachorro.alimentar();
// meuCachorro.emitirSom();
// //------------------------------------------------------------
// class Petzinho {
// constructor(nome, idade, tipo) {
// this.nome = nome;
// this.idade = idade;
// this.tipo = tipo;
// }
// getNome() {
// return this.nome;
// }
// setNome(novoNome) {
// this.nome = novoNome;
// }
// }
// const novoPetzinho = new Petzinho("Bob", 3, "Cachorro");
// console.log(novoPetzinho.getNome()); //Bob
// novoPetzinho.setNome("Rex");
// console.log(novoPetzinho.getNome()); //Rex
// console.log("--------------------Polimorfismo--------------------");
// class Animal {
// constructor(nome) {
// this.nome = nome;
// }
// fazerBarulho() {
// console.log("O animal faz um barulho genérico.");
// }
// }
// class Cachorro extends Animal {
// constructor(nome) {
// super(nome);
// }
// fazerBarulho() {
// console.log("O cachorro late!");
// }
// }
// class Gato extends Animal {
// constructor(nome) {
// super(nome);
// }
// fazerBarulho() {
// console.log("O gato mia!");
// }
// }
// // Exemplo de uso
// const animais = [
// new Cachorro("Rex"),
// new Gato("Mia"),
// new Animal("Bob"), // Animal genérico
// ];
// for (const animal of animais) {
// animal.fazerBarulho();
// }
console.log("----------------------Exemplos----------------------");
//Criando uma classe Retângulo
class Retangulo {
constructor(comprimento, largura) {
this.comprimento = comprimento;
this.largura = largura;
}
obeterPerimetro() {
return this.comprimento * 2 + this.largura * 2;
}
obeterArea() {
return this.comprimento * this.largura;
}
setComprimento(novoComprimento) {
this.comprimento = novoComprimento;
}
setLargura(novaLargura) {
this.largura = novaLargura;
}
}
// Criando um objeto do "tipo" Retângulo
var ret1 = new Retangulo(10, 15)
function displayRet(retangulo){
console.table(retangulo);
console.log(`comprimento = ${retangulo.comprimento}`)
console.log(`largura = ${retangulo.largura}`)
console.log(`Área = ${retangulo.obeterArea()}`)
console.log(`Perimetro = ${retangulo.obeterPerimetro()}`)
}
displayRet (ret1)
ret1.setComprimento(20)
displayRet(ret1)
ret1.setLargura(20)
displayRet(ret1)