Presentation is loading. Please wait.

Presentation is loading. Please wait.

import math class Circulo: def __init__(self,x): if x<=0: exit("radio<=0”) self.r=x def perimetro(self): return 2*math.pi*self.r def area(self):

Similar presentations


Presentation on theme: "import math class Circulo: def __init__(self,x): if x<=0: exit("radio<=0”) self.r=x def perimetro(self): return 2*math.pi*self.r def area(self):"— Presentation transcript:

1

2

3

4

5

6 import math class Circulo: def __init__(self,x): if x<=0: exit("radio<=0”) self.r=x def perimetro(self): return 2*math.pi*self.r def area(self): return math.pi*self.r**2 class Cuadrado: def __init__(self,x): if x<=0: exit(“lado<=0”) self.a=x def perimetro(self): return 4*self.a def area(self): return self.a**2 Nota. Constructores son similares

7 Solución 2. Con Herencia import math class Figura: def __init__(self,x): #constructor único if x<=0: exit(“nº<=0") self.x=x #significado de x depende de figura class Circulo(Figura): def area(self): return math.pi*self.x**2 def perimetro(self): return 2*math.pi*self.x class Cuadrado(Figura): def area(self): return self.x**2 def perimetro(self): return 4*self.x

8 class Rectangulo(Figura): def __init__(self,x,y): #constructor Figura.__init__(self,x) #ctor clase Figura self.y=y #ancho if y<=0: exit(“nº<=0") def area(self): return self.x*self.y def perimetro(self): return 2*(self.x+self.y) Uso? r=Rectangulo(nº,nº) Otras figuras?

9 class Triangulo(Figura): def __init__(self,x,y,z): if x<=0 or y<=0 or z<=0 \ #continua… or x+y<=z or x+z<=y or y+z<=x: exit("no forman triangulo") self.x=x; self.y=y; self.z=z def perimetro(self): return self.x+self.y+self.z def area(self): s=(self.x+self.y+self.z)/2; return math.sqrt(s*(s-self.x)*(s-self.y)*(s-self.z)) Uso? t=Triangulo(nº,nº,nº)

10 r=raw_input(“circulo,cuadrado,rectangulo o ” + \ “ triangulo?").lower() if r=="circulo": f=Circulo(input("radio?")) elif r=="cuadrado": f=Cuadrado(input("lado?")) elif r=="rectangulo": f=Rectangulo(input("largo?"),\ input("ancho?")) elif r=="triangulo": f=Triangulo(input("lado1?"),\ input("lado2?"),\ input("lado3?")) else: exit("figura incorrecta") print "area=",f.area() print "perimetro=",f.perimetro()

11 Herencia “transitiva” (“sucesiva”) class A: def __init__(self,x): self.a=x def f(self): return self.a class B(A): #hereda de A def __init__(self,x,y): A.__init__(self,x) self.b=y def g(self): return self.b class C(B): #hereda de B def __init__(self,x,y,z): B.__init__(self,x,y) self.c=z def h(self): return self.c Uso? >>>c=C(1,2,3) >>>c.f()+c.g()+c.h() 6 A B C

12 Herencia “múltiple” class A: def __init__(self,x): self.a=x #una variable def f(self): return self.a class B: def __init__(self,x): self.b=x #una variable def g(self): return self.b class C(A,B): #C hereda de clases A y B def __init__(self,x,y,z): A.__init__(self,x) B.__init__(self,y) self.c=z #tres variables pq a y b se heredan def h(self): return self.c Uso? >>>c=C(1,2,3) >>>c.f()+c.g()+c.h() 6 AB C


Download ppt "import math class Circulo: def __init__(self,x): if x<=0: exit("radio<=0”) self.r=x def perimetro(self): return 2*math.pi*self.r def area(self):"

Similar presentations


Ads by Google