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()
print("Groarrr") class Dog:
def sound(self):
print("Woof woof!") def makeSound(animalType):
animalType.sound()
bearObj = Bear()
dogObj = Dog()
dogObj = Dog()
makeSound(bearObj)
makeSound(dogObj)
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)
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,
BearandDog, each having asoundmethod.
- The
soundmethod prints the corresponding animal sound ("Groarrr" for bear and "Woof woof!" for dog).
makeSound function:- This function takes an
animalTypeargument as input.
- Inside the function, we call the
soundmethod of theanimalTypeobject to produce the sound.
- We create instances (
bearObjanddogObj) of theBearandDogclasses, respectively.
- Then, we call the
makeSoundfunction twice, passingbearObjanddogObjas arguments. This invokes the correspondingsoundmethods for each object.
class Bear: def sound(self): print("Groarrr")
class Dog:
def sound(self):
print("Woof woof!")
def sound(self):
print("Woof woof!")
def makeSound(animalType):
animalType.sound()
animalType.sound()
print("Groarrr")
bearObj = Bear()
dogObj = Dog()
makeSound(bearObj)
makeSound(dogObj)
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