Kursinnhold
Object-Oriented Programming in Python
Object-Oriented Programming in Python
1. Classes and Objects
2. Encapsulation
3. Inheritance
4. Polymorphism
Implementation
Code
We start to fill the Cat
class:
- An attribute
name
with the predefined value'Kitty'
; - A method
say_meow()
.
We create an object cat
and call the say_meow()
method.
class Cat: # An attribute name = 'Kitty' # A method def say_meow(self): print('Meow') # Instantiating an object cat = Cat() cat.say_meow()
Note
- Each method obligatory contains
self
- a reference to the current object; - To call the method of the object, write
Object.Method()
.
Oppgave
Swipe to start coding
- Add the
sleep
method. - The
sleep
method prints a message. - Instantiate an object using the
cat
variable.
Løsning
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 1. Kapittel 3
Implementation
Code
We start to fill the Cat
class:
- An attribute
name
with the predefined value'Kitty'
; - A method
say_meow()
.
We create an object cat
and call the say_meow()
method.
class Cat: # An attribute name = 'Kitty' # A method def say_meow(self): print('Meow') # Instantiating an object cat = Cat() cat.say_meow()
Note
- Each method obligatory contains
self
- a reference to the current object; - To call the method of the object, write
Object.Method()
.
Oppgave
Swipe to start coding
- Add the
sleep
method. - The
sleep
method prints a message. - Instantiate an object using the
cat
variable.
Løsning
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 1. Kapittel 3