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

Svep för att visa menyn

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().

Uppgift

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 desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 3
Vi beklagar att något gick fel. Vad hände?

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

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().

Uppgift

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 desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 3
Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Vi beklagar att något gick fel. Vad hände?
some-alt