Dziedziczenie
Stworzymy klasę Point. Następnie klase Circle dziedziczącą z klasy Point oraz klasę Cylinder dziedziczącą z klasy Circle.
PRZYKŁAD 1:
class Point(object):
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __repr__(self):
return 'Point x={x} y={y}'.format(**self.__dict__)
p = Point(2,3)
print(p.__repr__())
PRZYKŁAD 2:
dodajemy metode 'move' oraz 'zamień pozycję'
class Point(object):
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __repr__(self):
return 'Point x={x} y={y}'.format(**self.__dict__)
""" metoda MOVE - przesuwa pozycje punktu o prametry"""
def move(self, move_x, move_y):
self.x += move_x
self.y += move_y
""" metoda zmien pozycje - przenosi punkt na nowe parametry"""
def change_position(self, new_x, new_y):
self.x=new_x
self.y=new_y
p = Point(2,3)
print(p)
p.move(5,8)
print(p)
p.change_position(5,5)
print(p)
PRZYKŁAD 3:
Dodajemy klasę Circle
class Point(object):
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __repr__(self):
return 'Point x={x} y={y}'.format(**self.__dict__)
""" metoda MOVE - przesuwa pozycje punktu o prametry"""
def move(self, move_x, move_y):
self.x += move_x
self.y += move_y
""" metoda zmien pozycje - przenosi punkt na nowe parametry"""
def change_position(self, new_x, new_y):
self.x=new_x
self.y=new_y
class Circle(Point):
def __init__(self, x, y, radius=1):
super(Circle, self).__init__(x, y)
self.radius = radius
def __repr__(self):
return 'Circle x={x} y={y} redius={radius}'.format(**self.__dict__)
p = Point(2,3)
print(p)
p.move(5,8)
print(p)
p.change_position(5,5)
print(p)
c=Circle(21,22,7)
print(c)
c.move(3,3)
print(c)
PRZYKŁAD 4:
sprawdzmy czy punkt leży wewnątrz okręgu
class Point(object):
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __repr__(self):
return 'Point x={x} y={y}'.format(**self.__dict__)
""" metoda MOVE - przesuwa pozycje punktu o prametry"""
def move(self, move_x, move_y):
self.x += move_x
self.y += move_y
""" metoda zmien pozycje - przenosi punkt na nowe parametry"""
def change_position(self, new_x, new_y):
self.x=new_x
self.y=new_y
class Circle(Point):
def __init__(self, x, y, radius=1):
super(Circle, self).__init__(x, y)
self.radius = radius
def __repr__(self):
return 'Circle x={x} y={y} redius={radius}'.format(**self.__dict__)
#sprawdzamy czy punkt lezy wewnatrz okregu x**2+y**2=r**2 x**2(x do potegi 2)
def is_in(self,x,y):
return((self.x - x)**2 + (self.y - y)**2) < (self.radius)**2
p = Point(2,3)
print(p)
p.move(5,8)
print(p)
p.change_position(5,5)
print(p)
c=Circle(21,22,7)
print(c)
c.move(3,3)
print(c)
#sprawdzamy czy pkt lezy wewnatrz okregu
print(c.is_in(10,10))
print(c.is_in(25,25))
PRZYKŁAD 5:
do powyższych przykładów dodajemy medtodę -> oblicznie pola koła
class Point(object):
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __repr__(self):
return 'Point x={x} y={y}'.format(**self.__dict__)
""" metoda MOVE - przesuwa pozycje punktu o prametry"""
def move(self, move_x, move_y):
self.x += move_x
self.y += move_y
""" metoda zmien pozycje - przenosi punkt na nowe parametry"""
def change_position(self, new_x, new_y):
self.x=new_x
self.y=new_y
class Circle(Point):
def __init__(self, x, y, radius=1):
super(Circle, self).__init__(x, y)
self.radius = radius
def __repr__(self):
return 'Circle x={x} y={y} redius={radius}'.format(**self.__dict__)
#sprawdzamy czy punkt lezy wewnatrz okregu x**2+y**2=r**2 x**2(x do potegi 2)
def is_in(self,x,y):
return((self.x - x)**2 + (self.y - y)**2) < (self.radius)**2
def pole(self, x, y, radius):
self.pole = 6.14*radius
return 'Pole koła o wsp x={x} y={y} r={radius} wynosi: {pole}'.format(**self.__dict__)
#return pole
p = Point(2,3)
print(p)
p.move(5,8)
print(p)
p.change_position(5,5)
print(p)
c=Circle(21,22,7)
print(c)
c.move(3,3)
print(c)
#sprawdzamy czy pkt lezy wewnatrz okregu
print(c.is_in(10,10))
print(c.is_in(25,25))
print(c.pole(1,2,3))







