Wednesday, April 17, 2024

Python Tutorial - Polymorphism - 019

 Polymorphism adalah konsep yang fundamental dalam object-oriented programming.  Kata Polymorphism merujuk pada classes yang berbeda yang diperlakukan sebagai class yang sama melalui inheritance.


Image - Polymorphism (credit to CodersArcade)

Syntax Error:
Class Bear:
def sound(self):
print("Groarrr") class Dog:
def sound(self):
print("Woof woof!") def makeSound(animalType):
animalType.sound()

bearObj = Bear()
dogObj = Dog()

makeSound(bearObj)
makeSound(dogObj)


True Correction:
class Bear: def sound(self): print("Groarrr") class Dog: def sound(self): print("Woof woof!") def makeSound(animal): animal.sound() bearObj = Bear() dogObj = Dog() makeSound(bearObj) makeSound(dogObj)

Output:
Groarrr
Woof woof!

In Python, indentation is crucial for defining code blocks. Here's a breakdown of the corrected code:
Classes:
  • We define two classes, Bear and Dog, each having a sound method.
  • The sound method prints the corresponding animal sound ("Groarrr" for bear and "Woof woof!" for dog).
makeSound function:
  • This function takes an animalType argument as input.
  • Inside the function, we call the sound method of the animalType object to produce the sound.
Creating objects and calling functions:
  • We create instances (bearObj and dogObj) of the Bear and Dog classes, respectively.
  • Then, we call the makeSound function twice, passing bearObj and dogObj as arguments. This invokes the corresponding sound methods for each object.

class Bear: def sound(self): print("Groarrr")

class Dog:
def sound(self):
print("Woof woof!")

def makeSound(animalType):
animalType.sound()
    print("Groarrr")

bearObj = Bear()
dogObj = Dog()

makeSound(bearObj)
makeSound(dogObj)

"When creating applications like a document editor, you may not know beforehand the types of documents a user will open, be it PDF or Word. Rather than having separate types for each document format, wouldn’t it be convenient to access them uniformly?"
"By defining an abstract class, we can outline a structure that every derived class must adhere to. Here’s how it works:"
class Document:
def __init__(self, name):
self.name = name
def show(self):
raise NotImplementedError("Subclass must implement this abstract method")
==========================

Dalam Python, Polymorphism diekskusi dengan berbagai cara, termasuk menggunakan functions dan abstract classes.


Function-Based Polymorphism in Python
    Semisal, ada dua classes, yaitu: Bear and Dog. Setiap class ini punya sebuah metode yang menghasilkan bunyi unik. 
    Menggunakan Polymorphism, kita bisa menggunakan metode sound untuk object  class ini melalui function umum.

SyntaxError:  
class Bear(object):
    def sound(self):
        print("Groarrr")

        class Dog(object):
    def sound(self):
        print("Woof woof!")

        def makeSound(animalType):
    animalType.sound()

    bearObj = Bear()
dogObj = Dog()

makeSound(bearObj)
makeSound(dogObj)

Correction program:



Error and true
class Bear(object):
    def sound(self):
        print("Groarrr")
class Bear:
def sound(self):
    print("Groarrr")

 class Dog(object):
    def sound(self):
        print("Woof woof!")

        def makeSound(animalType):
    animalType.sound()

bearObj = Bear()
dogObj = Dog()

makeSound(bearObj)
makeSound(dogObj)
--------------------------------------------

Abstract Class-Based Polymorphism




class Document: def __init__(self, name): self.name = name def show(self): raise NotImplementedError("Subclass must implement this abstract method")

class Pdf(Document): def show(self): return 'Show pdf contents!' class Word(Document): def show(self): return 'Show word contents!' documents = [Pdf('Document1'), Pdf('Document2'), Word('Document3')] for document in documents: print(document.name + ': ' + document.show())

Output:
Document1: Show pdf contents!
Document2: Show pdf contents!
Document3: Show word contents!'
# Posting sebelumnya:
JJJ

No comments:

Post a Comment