Skip to content

Commit 82ad8d4

Browse files
authored
Merge pull request mouredev#7645 from JuSeRDev/JuSeRDev
[08]-TypeScript
2 parents e9afed1 + 746e31d commit 82ad8d4

File tree

1 file changed

+351
-0
lines changed

1 file changed

+351
-0
lines changed
+351
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,351 @@
1+
// # #08 CLASES
2+
// > #### Dificultad: Fácil | Publicación: 19/02/24 | Corrección: 26/02/24
3+
4+
// ## Ejercicio
5+
6+
7+
/*
8+
* EJERCICIO:
9+
* Explora el concepto de clase y crea un ejemplo que implemente un inicializador,
10+
* atributos y una función que los imprima (teniendo en cuenta las posibilidades
11+
* de tu lenguaje).
12+
* Una vez implementada, créala, establece sus parámetros, modifícalos e imprímelos
13+
* utilizando su función.
14+
*
15+
* DIFICULTAD EXTRA (opcional):
16+
* Implementa dos clases que representen las estructuras de Pila y Cola (estudiadas
17+
* en el ejercicio número 7 de la ruta de estudio)
18+
* - Deben poder inicializarse y disponer de operaciones para añadir, eliminar,
19+
* retornar el número de elementos e imprimir todo su contenido.
20+
*
21+
*/
22+
23+
24+
//Estructura basica de Clases
25+
26+
namespace clase8 {
27+
class Persona { // Porpiedad
28+
nombre: string;
29+
30+
constructor(nombre: string){
31+
this.nombre = nombre //Inicializacion
32+
}
33+
34+
saludar(): string{ //metodo - esto es como una funcion
35+
return `¡Hola! soy ${this.nombre}`
36+
}
37+
}
38+
39+
//crear una instancia
40+
const Persona1 = new Persona("Sebastian")
41+
console.log(Persona1.saludar()) //¡Hola! soy Sebastian
42+
43+
44+
//Modificadores de acceso
45+
46+
class Banco {
47+
public nombreBanco: string
48+
private balanceBanco: number
49+
50+
constructor(Nombre: string, balance: number){
51+
this.nombreBanco = Nombre
52+
this.balanceBanco = balance
53+
}
54+
55+
public consultarBalance(): string{
56+
return `el balance es $${this.balanceBanco}`
57+
}
58+
59+
private actualizarBalance(cantidad: number): void {
60+
this.balanceBanco += cantidad
61+
}
62+
}
63+
64+
const banco1 = new Banco("Banco Central", 1000)
65+
console.log(banco1.consultarBalance())
66+
67+
//banco1.actualizarBalance(500) //la propidad actualizarBalance es privada
68+
69+
//Metodos y propieaddes estaticas
70+
71+
class Matematicas {
72+
static PI: number = 3.1416
73+
74+
static calcularCircunferencia(radio: number): number{
75+
return 2 * this.PI * radio
76+
}
77+
}
78+
79+
console.log(Matematicas.PI)
80+
console.log(Matematicas.calcularCircunferencia(5))
81+
82+
// Herencia
83+
84+
class Animal {
85+
nombre: string
86+
87+
constructor(nombre: string) {
88+
this.nombre = nombre
89+
}
90+
91+
emitirSonido(): string {
92+
return `Hace un sonido generico`
93+
}
94+
}
95+
96+
class Perro extends Animal {
97+
emitirSonido(): string {
98+
return `¡Guau!`
99+
}
100+
}
101+
102+
class Pollito extends Animal{
103+
emitirSonido(): string {
104+
return `Pio pio pio!!`
105+
}
106+
}
107+
108+
const gato = new Animal("Michi")
109+
console.log(gato.nombre)
110+
console.log(gato.emitirSonido())
111+
112+
const perro = new Perro("Firulais")
113+
console.log(perro.nombre)
114+
console.log(perro.emitirSonido())
115+
116+
const pollito = new Pollito("Theo")
117+
console.log(pollito.nombre)
118+
console.log(pollito.emitirSonido())
119+
120+
// Clases abstractas
121+
122+
abstract class Figura {
123+
abstract calcularArea(): number
124+
125+
description(): string {
126+
return `Soy una figura geometria`
127+
}
128+
}
129+
130+
class Cuadrado extends Figura {
131+
lado: number
132+
133+
constructor(lado: number){
134+
super()
135+
this.lado = lado
136+
}
137+
138+
calcularArea(): number {
139+
return this.lado ** 2
140+
}
141+
}
142+
143+
144+
const cuadrado = new Cuadrado(4)
145+
console.log(cuadrado.description())
146+
console.log(cuadrado.calcularArea())
147+
148+
149+
// Interface vs Calses
150+
151+
interface Vehiculo {
152+
marca: string
153+
modelo: string
154+
conducir(): void
155+
}
156+
157+
class Coche implements Vehiculo {
158+
marca: string
159+
modelo: string
160+
161+
constructor(marca: string, modelo: string){
162+
this.marca = marca
163+
this.modelo = modelo
164+
}
165+
166+
conducir(): void {
167+
console.log(`Conduiendo un ${this.marca} modelo ${this.modelo}`)
168+
return
169+
}
170+
}
171+
172+
const coche = new Coche("toyota", "corola")
173+
coche.conducir()
174+
175+
// Ejemplo practico mas largo
176+
177+
class Personas {
178+
nombre: string
179+
edad: number
180+
ocupacion: string
181+
182+
constructor(nombre: string, edad: number, ocupacion: string) {
183+
this.nombre = nombre
184+
this.edad = edad
185+
this.ocupacion = ocupacion
186+
}
187+
188+
saludar(): string{
189+
return `¡Hola! soy ${this.nombre}`
190+
}
191+
192+
presentarse(): string {
193+
return `Me llamo ${this.nombre}, tengo ${this.edad} y soy ${this.ocupacion}.`
194+
}
195+
196+
cumplirAnios(): void {
197+
this.edad++
198+
}
199+
}
200+
201+
const personas1 = new Personas("Ana", 25, "desarrolladora Web")
202+
const personas2 = new Personas("Sebastian", 33, "desarrollador web")
203+
204+
205+
console.log(personas1.saludar())
206+
console.log(personas1.presentarse())
207+
208+
console.log(personas2.nombre)
209+
console.log(personas2.edad)
210+
console.log(personas2.ocupacion)
211+
console.log(personas2.presentarse())
212+
213+
personas2.cumplirAnios()
214+
console.log(personas2.presentarse())
215+
216+
217+
218+
//! SOLUCION DEL PROBLEMA
219+
220+
class Programer {
221+
name: string
222+
sername?: string
223+
age: number
224+
lenguajes: string[]
225+
226+
constructor(name: string, age: number, lenguajes: string[], sername?: string){
227+
this.name = name
228+
this.sername = sername
229+
this.age = age
230+
this.lenguajes = lenguajes
231+
}
232+
233+
print(): string{
234+
return `nombre: ${this.name}${this.sername ? ", Apellido: " + this.sername : ""}, edad: ${this.age}, lenguajes: ${this.lenguajes}`
235+
}
236+
237+
}
238+
239+
const sebastian = new Programer("Sebastian", 30, ["JavaScipt", "TypeScript"])
240+
const juan = new Programer("juan", 33, ["JavaScipt", "TypeScript"], "rocha")
241+
242+
console.log(sebastian.print())
243+
console.log(juan.print())
244+
245+
sebastian.age = 33 //modifique la edad
246+
247+
console.log(sebastian.print()) //edad modificada
248+
249+
//! DIFICULTAD EXTRA
250+
251+
class Stack {
252+
stack: any[]
253+
254+
constructor(){
255+
this.stack = []
256+
}
257+
258+
push(item){
259+
this.stack.push(item)
260+
return this.stack
261+
}
262+
263+
pop(){
264+
if (this.count() === 0) {
265+
return "no hay nada que borrar"
266+
}
267+
return this.stack.pop()
268+
}
269+
270+
count(){
271+
return this.stack.length
272+
}
273+
274+
print(){
275+
if (this.count() > 0) {
276+
return this.stack.slice().reverse()
277+
}
278+
return "El Stack esta vacio"
279+
}
280+
}
281+
282+
const stack = new Stack()
283+
284+
stack.push("A")
285+
stack.push("B")
286+
stack.push("C")
287+
console.log(stack.count())
288+
console.log(stack.print())
289+
console.log(stack.pop())
290+
console.log(stack.count())
291+
console.log(stack.print())
292+
console.log(stack.pop())
293+
console.log(stack.count())
294+
console.log(stack.print())
295+
console.log(stack.pop())
296+
console.log(stack.count())
297+
console.log(stack.print())
298+
299+
300+
class Queue {
301+
queue: any[]
302+
303+
constructor(){
304+
this.queue = []
305+
}
306+
307+
push(item){
308+
this.queue.push(item)
309+
return this.queue
310+
}
311+
312+
shift(){
313+
if (this.count() === 0) {
314+
return "no hay nada que borrar"
315+
}
316+
return this.queue.shift()
317+
}
318+
319+
count(){
320+
return this.queue.length
321+
}
322+
323+
print(){
324+
if (this.count() > 0) {
325+
return this.queue.slice()
326+
}
327+
return "El queue esta vacio"
328+
}
329+
}
330+
331+
const myQueue = new Queue()
332+
333+
myQueue.push("A")
334+
myQueue.push("B")
335+
myQueue.push("C")
336+
console.log(myQueue.count())
337+
console.log(myQueue.print())
338+
console.log(myQueue.shift())
339+
console.log(myQueue.count())
340+
console.log(myQueue.print())
341+
console.log(myQueue.shift())
342+
console.log(myQueue.count())
343+
console.log(myQueue.print())
344+
console.log(myQueue.shift())
345+
console.log(myQueue.count())
346+
console.log(myQueue.print())
347+
console.log(myQueue.shift())
348+
console.log(myQueue.count())
349+
console.log(myQueue.print())
350+
351+
}

0 commit comments

Comments
 (0)