Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Implementation | Classes and Objects
Object-Oriented Programming in Python
course content

Kursusindhold

Object-Oriented Programming in Python

Object-Oriented Programming in Python

1. Classes and Objects
2. Encapsulation
3. Inheritance
4. Polymorphism

book
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.

12345678910
class Cat: # An attribute name = 'Kitty' # A method def say_meow(self): print('Meow') # Instantiating an object cat = Cat() cat.say_meow()
copy

Note

  • Each method obligatory contains self - a reference to the current object;
  • To call the method of the object, write Object.Method().
Opgave

Swipe to start coding

  1. Add the sleep method.
  2. The sleep method prints a message.
  3. Instantiate an object using the cat variable.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 3
toggle bottom row

book
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.

12345678910
class Cat: # An attribute name = 'Kitty' # A method def say_meow(self): print('Meow') # Instantiating an object cat = Cat() cat.say_meow()
copy

Note

  • Each method obligatory contains self - a reference to the current object;
  • To call the method of the object, write Object.Method().
Opgave

Swipe to start coding

  1. Add the sleep method.
  2. The sleep method prints a message.
  3. Instantiate an object using the cat variable.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 3
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt