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: %% Cell type:code id: tags:
``` ```
class Car: class Car:
def __init__( self, name, color, year): def __init__( self, name, color, year):
self.name = name self.name = name
self.color = color self.color = color
self.year = year self.year = year
def present( self ): def present( self ):
return 'Hey! I am '+self.name+', I am '+self.color+' and I appeared in '+str(self.year) return 'Hey! I am '+self.name+', I am '+self.color+' and I appeared in '+str(self.year)
def be_painted( self, new_color ): def be_painted( self, new_color ):
self.color = new_color self.color = new_color
def love( self, other_car ): def love( self, other_car ):
return self.name+' is in love with '+other_car.name return self.name+' is in love with '+other_car.name
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
flash = Car( 'Flash McQueen', 'red', 2006 ) flash = Car( 'Flash McQueen', 'red', 2006 )
flash.present() flash.present()
``` ```
%% Output
'Hey! I am Flash McQueen, I am red and I appeared in 2006'
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
flash.be_painted( 'purple' ) flash.be_painted( 'purple' )
flash.present() flash.present()
``` ```
%% Output
'Hey! I am Flash McQueen, I am purple and I appeared in 2006'
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Exercise: ## 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. - 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. - call the method love of the Car class with Flash and Sally.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
``` ```
%% Output
'Hey! I am Sally Carrera, I am blue and I appeared in 2006'
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Inheritance ## Inheritance
https://www.geeksforgeeks.org/python-oops-concepts/#:~:text=In%20Python%2C%20object%2Doriented%20Programming,%2C%20etc.%20in%20the%20programming. https://www.geeksforgeeks.org/python-oops-concepts/#:~:text=In%20Python%2C%20object%2Doriented%20Programming,%2C%20etc.%20in%20the%20programming.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
# Parent class # Parent class
class Person(object): class Person(object):
# __init__ is known as the constructor # __init__ is known as the constructor
def __init__(self, name, idnumber): def __init__(self, name, idnumber):
self.name = name self.name = name
self.idnumber = idnumber self.idnumber = idnumber
def display(self): def display(self):
print(self.name) print(self.name)
print(self.idnumber) print(self.idnumber)
def details(self): def details(self):
print("My name is {}".format(self.name)) print("My name is {}".format(self.name))
print("IdNumber: {}".format(self.idnumber)) print("IdNumber: {}".format(self.idnumber))
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
# child class # child class
class Employee(Person): class Employee(Person):
def __init__(self, name, idnumber, salary, post): def __init__(self, name, idnumber, salary, post):
super().__init__(name, idnumber ) super().__init__(name, idnumber )
self.salary = salary self.salary = salary
self.post = post self.post = post
def details(self): def details(self):
print("My name is {}".format(self.name)) print("My name is {}".format(self.name))
print("IdNumber: {}".format(self.idnumber)) print("IdNumber: {}".format(self.idnumber))
print("Post: {}".format(self.post)) print("Post: {}".format(self.post))
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Exercise: ## Exercise:
- Create an object of the class Employee - Create an object of the class Employee
- Print his name and id number. - Print his name and id number.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` ```
# creation of an object variable or an instance # 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: %% 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