Skip to content
Snippets Groups Projects
Commit 25aaa13c authored by chloebt's avatar chloebt
Browse files

update 2023-2024

parent b6555a59
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags:
```
class Car:
def __init__( self, name, color, year):
self.name = name
self.color = color
self.year = year
def present( self ):
return 'Hey! I am '+self.name+', I am '+self.color+' and I appeared in '+str(self.year)
def be_painted( self, new_color ):
self.color = new_color
def love( self, other_car ):
return self.name+' is in love with '+other_car.name
```
%% Cell type:code id: tags:
```
flash = Car( 'Flash McQueen', 'red', 2006 )
flash.present()
```
%% Output
'Hey! I am Flash McQueen, I am red and I appeared in 2006'
%% Cell type:code id: tags:
```
flash.be_painted( 'purple' )
flash.present()
```
%% Output
'Hey! I am Flash McQueen, I am purple and I appeared in 2006'
%% Cell type:markdown id: tags:
## Exercise:
- define a new object of type Car with the following attributes: its name is Sally Carrera, its color is blue and it has the same year as the object my_car.
- call the method love of the Car class with Flash and Sally.
%% Cell type:code id: tags:
```
```
%% Output
'Hey! I am Sally Carrera, I am blue and I appeared in 2006'
%% Cell type:markdown id: tags:
## Inheritance
https://www.geeksforgeeks.org/python-oops-concepts/#:~:text=In%20Python%2C%20object%2Doriented%20Programming,%2C%20etc.%20in%20the%20programming.
%% Cell type:code id: tags:
```
# Parent class
class Person(object):
# __init__ is known as the constructor
def __init__(self, name, idnumber):
self.name = name
self.idnumber = idnumber
def display(self):
print(self.name)
print(self.idnumber)
def details(self):
print("My name is {}".format(self.name))
print("IdNumber: {}".format(self.idnumber))
```
%% Cell type:code id: tags:
```
# child class
class Employee(Person):
def __init__(self, name, idnumber, salary, post):
super().__init__(name, idnumber )
self.salary = salary
self.post = post
def details(self):
print("My name is {}".format(self.name))
print("IdNumber: {}".format(self.idnumber))
print("Post: {}".format(self.post))
```
%% Cell type:markdown id: tags:
## Exercise:
- Create an object of the class Employee
- Print his name and id number.
%% Cell type:code id: tags:
```
# creation of an object variable or an instance
emy = Employee('emily', 42, 90000, 'happy manager')
# calling a function
emy.display()
```
%% Output
# calling a function
emily
42
```
%% Cell type:code id: tags:
```
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment