1. Learn
  2. /
  3. Courses
  4. /
  5. Wprowadzenie do programowania obiektowego w Pythonie

Connected

Exercise

Porównywanie a dziedziczenie

W tym ćwiczeniu sprawdzimy, co się dzieje, gdy obiekt klasy nadrzędnej jest porównywany z obiektem klasy podrzędnej.

Rozważ dwie poniższe klasy, które zostały zaimplementowane i są dostępne w pliku script.py:

class Parent:
    def __eq__(self, other):
        print("Parent's __eq__() called")
        return True

class Child(Parent):
    def __eq__(self, other):
        print("Child's __eq__() called")
        return True

Klasa Child dziedziczy po klasie Parent. Obie implementują metodę __eq__(), która wyświetla komunikat diagnostyczny.

Instructions 1/1

undefined XP
    1

Question

  • Która metoda __eq__() zostanie wywołana po uruchomieniu poniższego kodu?*
p = Parent()
c = Child()

p == c 

Possible answers