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

Cursusinhoud

Object-Oriented Programming in Python

Object-Oriented Programming in Python

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

book
Unique Object

Code

Methods inside the class:

  • say_meow;
  • set_name with the name argument .

set_name defines an additional attribute name and sets the value of the given statement. self means that this variable is the class attribute, not the local variable.

Object cat is created. We call say_meow method and change the attribute of the class by referring to cat.hair_color. We set 'Princess' as the name using set_name method.

12345678910111213
class Cat: def say_meow(self): print('Meow') def set_name(self, name): self.name = name print(self.hair_color, 'cat name is', self.name) # Instantiating an object cat = Cat() cat.say_meow() # Creating the unique сat cat.hair_color = 'White' cat.set_name('Princess')
copy

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 4
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt