Skip to content

Commit aa575d6

Browse files
committed
Added OOP and modules changes
1 parent 7239bd9 commit aa575d6

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

oop/class.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ class Student:
33
year = 1
44
num_of_students = 0
55

6-
def __init__(self, first, last, email):
6+
def __init__(self, first, last, email): # Costruttore self è la referenza all'oggetto stesso (this di Java)
77
self.first = first
88
self.last = last
99
self.email = email
1010
Student.num_of_students += 1
1111

12-
def fullname(self):
13-
return '{} {}'.format(self.first, self.last)
12+
def fullname(self): # Metodo
13+
return '{} {}'.format(self.first, self.last) # self MUST be used
1414

1515
s1 = Student('Alessandro', 'Versace', 'aleversace98@gmail.com')
1616
s2 = Student('Me', "NotMe", "menotme@gmail.com")
@@ -22,10 +22,19 @@ def fullname(self):
2222
print('\n')
2323

2424
print(Student.__dict__)
25-
s1.year = 2
2625
print('\n')
2726

28-
print(s1.__dict__)
27+
s1.year = 2
28+
print(s1.__dict__)
2929
print('\n')
3030

31-
print(Student.num_of_students)
31+
print(Student.num_of_students)
32+
33+
# Interfaces: methods with "visibility" outside the object.
34+
35+
# Encapsulation: restrict access of some components (Then we can control access with methods ie. get() and set())
36+
37+
# Inheritance: Sub-Classes inherit attributes and methods from their "parents" Classes (Super Classes).
38+
# A Super class, B sub-class -> We say "B extends A".
39+
# Can be single or multiple.
40+

oop/exceptions.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### EXCEPTIONS
2+
3+
x = -1
4+
# Handling
5+
try:
6+
if x < 0:
7+
raise ValueError("Negative amount") # Launch
8+
except ValueError:
9+
print("Value Error")
10+
except:
11+
print("An exception occurred")
12+
else:
13+
print("Nothing went wrong")

oop/point.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Point:
2+
3+
def __init__(self, name, x, y):
4+
self.name = name # Public Access from everywhere.
5+
self._x = x # _ Protected Access from inside or from sub-classes or proprial methods.
6+
self.__y = y # __ Private Access just with proprial methods.
7+
8+
def getY(self):
9+
return self.__y
10+
11+
def setXY(self, x, y):
12+
self._x = x
13+
self.__y = y
14+
15+
def printXY(self):
16+
print('x = {}, y = {}'.format(self._x, self.__y))
17+
18+
p1 = Point("uno", 1, 1)
19+
print(p1.name)
20+
# print(p1.x) WRONG We have to use implement proper method!
21+
# print(p1.y) WRONG Same here
22+
p1.printXY()
23+
24+

oop/point3D.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from point import *
2+
3+
class Point3D(Point): # With multiple inheritance and conflicts, wins the lefter ones.
4+
_z = 0
5+
def __init__(self, x, y, z):
6+
Point.__init__(self, 'Broo', x, y)
7+
self._z = z
8+
9+
def describe(self):
10+
return 'Point3d name:{} ({}, {}, {})'.format(self.name, self._x, self.getY(), self._z)
11+
12+
p1 = Point3D(1, 2, 3)
13+
print(p1.describe())

0 commit comments

Comments
 (0)