-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFinal_Examples.py
More file actions
50 lines (45 loc) · 1.24 KB
/
Final_Examples.py
File metadata and controls
50 lines (45 loc) · 1.24 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
#Final Project
"""
OOP: Object Oriented Programing
-class/object
-attributes/methods
-encapsulation/ abstraciton
-inheritance
-overriding/polymorphism
"""
from abc import ABC , abstractmethod
class Shape(ABC):
def area(self): pass
@abstractmethod
def perimeter(self): pass
@abstractmethod
def toString(self): pass
class Square(Shape):
def __init__(self, edge):
self.__edge = edge # encapsulation private attribute
def area(self):
result = self.__edge**2
print("Square area: ",result)
def perimeter(self):
result = self.__edge*4
print("Square perimeter: ",result)
# override and polymorphism
def toString(self):
print("Square edge: ",self.__edge)
# child
class Circle(Shape):
PI = 3.14
def __init__(self, radius):
self.__radius = radius
def area(self):
result = self.PI*self.__radius**2 # pi*r^2
print("Circle area: ",result)
def perimeter(self):
result = 2*self.PI*self.__radius # 2*pi*r
print("Circle perimeter: ",result)
def toString(self):
print("Circle radius: ",self.__radius)
a = Circle(5)
a.area()
a.perimeter()
a.toString()