forked from joaoventura/full-speed-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.tex
More file actions
81 lines (49 loc) · 3.86 KB
/
classes.tex
File metadata and controls
81 lines (49 loc) · 3.86 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
\chapter{Classes}\label{classes}
In object oriented programming (OOP), a class is a structure that allows to group together a set of properties (called attributes) and functions (called methods) to manipulate those properties. Take the following class that defines a person with properties "name" and "age" and the "greet" method.
\begin{lstlisting}
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print("Hello, my name is %s!" % self.name)
\end{lstlisting}
Most classes will need the constructor method ("\_\_init\_\_") to initialize the class's attributes. In the previous case the constructor of the class receives the person's name and age and stores that information in the class's instance (referenced by the \textit{self} keyword). Finally, "greet" method prints the name of the person as stored in a specific class instance (object).
Class instances are used through the instantiation of objects. Here's how we can instantiate two objects:
\begin{lstlisting}
>>> a = Person("Peter", 20)
>>> b = Person("Anna", 19)
>>> a.greet()
Hello, my name is Peter!
>>> b.greet()
Hello, my name is Anna!
>>> print(a.age) # We can also access the attributes of an object
20
\end{lstlisting}
\section{Exercises with classes}
Use the Python documentation on classes at \url{https://docs.python.org/3/tutorial/classes.html} to solve the following exercises.
\begin{enumerate}
\item Implement a class named "Rectangle" to store the coordinates of a rectangle given by (x1, y1) and (x2, y2).
\item Implement the class constructor with the parameters (x1, y1, x2, y2) and store them in the class instances using the "self" keyword.
\item Implement the "width()" and "height()" methods which return, respectively, the width and height of a rectangle. Create two objects, instances of "Rectangle" to test the calculations.
\item Implement the method "area" to return the area of the rectangle (width*height).
\item Implement the method "circumference" to return the perimeter of the rectangle (2*width + 2*height).
\item Do a print of one of the objects created to test the class. Implement the "\_\_str\_\_" method such that when you print one of the objects it print the coordinates as (x1, y1)(x2, y2).
\end{enumerate}
\section{Class inheritance}
In object oriented programming, inheritance is one of the forms in which a subclass can inherit the attributes and methods of another class, allowing it to rewrite some of the super class's functionalities. For instance, from the "Person" class above we could create a subclass to keep people with 10 years of age:
\begin{lstlisting}
class TenYearOldPerson(Person):
def __init__(self, name):
super().__init__(name, 10)
def greet(self):
print("I don't talk to strangers!!")
\end{lstlisting}
The indication that the "TenYearOldPerson" class is a subclass of "Person" is given on the first line. Then, we rewrote the constructor of the subclass to only receive the name of the person, but we will eventually call the super class's constructor with the name of the 10-year-old and the age hardcoded as 10. Finally we reimplemented the "greet" method.
\section{Exercises with inheritance}
Use the "Rectangle" class as implemented above for the following exercises:
\begin{enumerate}
\item Create a "Square" class as subclass of "Rectangle".
\item Implement the "Square" constructor. The constructor should have only the x1, y1 coordinates and the size of the square. Notice which arguments you'll have to use when you invoce the "Rectangle" constructor when you use "super".
\item Instantiate two objects of "Square", invoke the area method and print the objects. Make sure that all calculations are returning correct numbers and that the coordinates of the squares are consistent with the size of the square used as argument.
\end{enumerate}