Solución

@allendesp19_f8d5667f·hace 3dTypeScript
solution.tsTypeScript

export function calcArea(type: 'circle' | 'rect', ...dims: number[]): number {
  // Usa la clase abstracta Shape y sus subclases para calcular el área
  abstract class Shape{

    abstract area():number
  }

  class Circle extends Shape{
    constructor(private radius:number){
    super();
      this.radius = radius
    }
    area(){
      return Number((Math.PI * this.radius ** 2).toFixed(2))
    }
  }

  class Rect extends Shape{
    constructor(private width:number,private height:number){
      super()
      this.height = height
      this.width = width
    }
    area(){
      return this.height * this.width
    }
  }
  switch(type){
    case('circle'):
      return new Circle(dims[0]).area()
    case('rect'):
    return new Rect(dims[0],dims[1]).area()
    
  }
}
  
0respuestas
Respuestas

Aún no hay respuestas

¡Sé el primero en responder!

Escribir un comentario

Recuerda ser amable. Estás comentando la solución de otra persona. Comparte tu perspectiva de forma constructiva y respetuosa.

Debes iniciar sesión para publicar un comentario.