BaşlayınÜcretsiz Başlayın

Class-level attributes

Class attributes store data that is shared among all the class instances. They are assigned values in the class body and are referred to using the ClassName. syntax rather than self. syntax when used in methods.

In this exercise, you will be a game developer working on a game that will have several players moving on a grid and interacting with each other. As the first step, you want to define a Player class that will just move along a straight line. Player will have a position attribute and move along a grid with a limited number of positions, so the position of Player will have a maximal value.

Bu egzersiz

Introduction to Object-Oriented Programming in Python

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • Define a Player class.
  • Create a class attribute called MAX_POSITION with value of 10.
  • In the __init__() constructor, set the attribute position that assigns to an object to 0.
  • Create a Player object p and print its MAX_POSITION.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

# Create a Player class
____:
  
  # Create MAX_POSITION class attribute
  ____ = ____
  
  # Add a constructor, setting position to zero
  ____:
    ____.____ = ____

# Create a player p and print its MAX_POSITION
p = ____()
print(____.____)
Kodu Düzenle ve Çalıştır