0Pricing
Learn AI with Python · レッスン

ポリモーフィズム

ポリモーフィズムによって柔軟で動的なコードを実現する方法を学びます。

「ポリモーフィズム」はCoddyKit上の無料Learn AI with Pythonレッスンです。 これはレッスン4/6です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはLearn AI with Python学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Learn AI with Pythonコースには全6レッスンが含まれています。

ポリモーフィズム入門

ポリモーフィズム入門

ポリモーフィズムは、異なるクラスのオブジェクトを共通のスーパークラスのオブジェクトとして扱えるようにする、オブジェクト指向プログラミング(OOP)の基本概念です。コードに柔軟性と再利用性をもたらします。

このレッスンでは、メソッドとクラスを使ってPythonでポリモーフィズムを実装する方法を学びます。

ポリモーフィズム — イラスト1

ポリモーフィズムとは

ポリモーフィズムとは

ポリモーフィズムを使うと、異なるクラスのメソッドが同じ名前を持ちながら、それぞれ異なる動作を実行できます。これにより、コードの柔軟性が高まり、拡張もしやすくなります。

# Defining a superclass
class Animal:
    # Method in the superclass
    def speak(self):
        print("Animal speaks")  # Generic message for all animals

# Defining a subclass that overrides the speak method
class Dog(Animal):
    def speak(self):
        print("Dog barks")  # Specific behavior for Dog

メソッドによるポリモーフィズム

メソッドによるポリモーフィズム

異なるクラスに同じ名前のメソッドを定義し、それぞれ異なる実装を提供できます。

# Another subclass with its own implementation of the speak method
class Cat(Animal):
    def speak(self):
        print("Cat meows")  # Specific behavior for Cat

# Function demonstrating polymorphism
# It can accept objects of any subclass of Animal

def animal_sound(animal):
    animal.speak()  # Calls the appropriate speak method based on the object type

# Creating objects of Dog and Cat
dog = Dog()
cat = Cat()

# Calling the function with different objects
animal_sound(dog)  # Outputs: Dog barks
animal_sound(cat)  # Outputs: Cat meows

メソッドによるポリモーフィズムの例

メソッドによるポリモーフィズム

異なるクラスに同じ名前のメソッドを定義し、それぞれ異なる実装を提供できます。

# Another subclass with its own implementation of the speak method
class Cat(Animal):
    def speak(self):
        print("Cat meows")  # Specific behavior for Cat

# Function demonstrating polymorphism
# It can accept objects of any subclass of Animal

def animal_sound(animal):
    animal.speak()  # Calls the appropriate speak method based on the object type

# Creating objects of Dog and Cat
dog = Dog()
cat = Cat()

# Calling the function with different objects
animal_sound(dog)  # Outputs: Dog barks
animal_sound(cat)  # Outputs: Cat meows

関数によるポリモーフィズム

関数によるポリモーフィズム

ポリモーフィズムを使うと、同じ関数で異なる型のオブジェクトを操作できます。

# Defining two unrelated classes
class Bird:
    def fly(self):
        print("Bird flies")  # Behavior specific to birds

class Plane:
    def fly(self):
        print("Plane flies")  # Behavior specific to planes

# Function demonstrating polymorphism
# It can accept objects of both Bird and Plane

def flying_object(obj):
    obj.fly()  # Calls the appropriate fly method based on the object type

# Creating objects of Bird and Plane
bird = Bird()
plane = Plane()

# Calling the function with different objects
flying_object(bird)  # Outputs: Bird flies
flying_object(plane)  # Outputs: Plane flies

継承によるポリモーフィズム

継承によるポリモーフィズム

ポリモーフィズムは、多くの場合、サブクラスがスーパークラスのメソッドをオーバーライドする継承によって実現されます。

# Superclass representing a generic vehicle
class Vehicle:
    def start(self):
        print("Vehicle starts")  # Generic behavior for all vehicles

# Subclass representing a specific type of vehicle
class Car(Vehicle):
    def start(self):
        print("Car starts")  # Specific behavior for cars

# Subclass representing another specific type of vehicle
class Bike(Vehicle):
    def start(self):
        print("Bike starts")  # Specific behavior for bikes

# Function demonstrating polymorphism
# It can accept objects of any subclass of Vehicle

def start_vehicle(vehicle):
    vehicle.start()  # Calls the appropriate start method based on the object type

# Creating objects of Car and Bike
car = Car()
bike = Bike()

# Calling the function with different objects
start_vehicle(car)  # Outputs: Car starts
start_vehicle(bike)  # Outputs: Bike starts

ポリモーフィズムの利点

ポリモーフィズムの利点

ポリモーフィズムには、次のような利点があります。

  • コードを再利用でき、柔軟性が高まります。
  • コードの可読性と構成が向上します。
  • 共通インターフェースの利用を促進します。

実世界の例

実世界の例

ポリモーフィズムを使った実世界の例を見てみましょう。

# Real-world example of polymorphism
class Developer:
    def work(self):
        print("Writing code")  # Specific task for developers

class Designer:
    def work(self):
        print("Designing user interfaces")  # Specific task for designers

# Function demonstrating polymorphism
# It can accept objects of both Developer and Designer

def workday(employee):
    employee.work()  # Calls the appropriate work method based on the object type

# Creating objects of Developer and Designer
developer = Developer()
designer = Designer()

# Calling the function with different objects
workday(developer)  # Outputs: Writing code
workday(designer)  # Outputs: Designing user interfaces

ポリモーフィズムでよくある間違い

ポリモーフィズムでよくある間違い

ここでは、避けるべき間違いを紹介します。

  • 必要な場合にサブクラスでメソッドをオーバーライドし忘れること。
  • 多重継承におけるメソッド解決順序(MRO)を混同すること。
  • 単純なタスクにポリモーフィズムを不必要に使用すること。

学んだこと

学んだこと

このレッスンでは、次のことを学びました。

  • ポリモーフィズムとは何か、Pythonでどのように機能するか。
  • メソッドと継承を使ってポリモーフィズムを実装する方法。
  • OOPでポリモーフィズムを利用する利点。

よくできました。次のトピックに進みましょう。

ポリモーフィズム — イラスト12

よくある質問

「ポリモーフィズム」レッスンは無料ですか?

はい。「ポリモーフィズム」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Learn AI with Pythonコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Learn AI with Pythonコースには全6レッスンが含まれています。

「ポリモーフィズム」で何を学びますか?

ポリモーフィズムによって柔軟で動的なコードを実現する方法を学びます。 ブラウザで直接実行するハンズオンコードでLearn AI with Pythonを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Learn AI with Pythonを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのLearn AI with Pythonは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/6です。

「ポリモーフィズム」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このLearn AI with Pythonレッスンでコードを書いて実行できますか?

はい。すべてのLearn AI with Pythonレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. クラスとオブジェクト
  2. 属性とメソッド
  3. 継承
  4. ポリモーフィズム
  5. カプセル化と抽象化
  6. マジックメソッド(__str__、__repr__ など)
← Learn AI with Pythonに戻る