Polymorphism
Polymorphism means different types of response. Polymorphism is so profitable because it allows many programs that are so light and easy. ... Python offers a contract that is under the subdivision. These learning processes consist of the formation of various types of substances.
Sometimes something comes in many forms. If we have a button, there are many things to come out to draw (circle buttons, pods, buttons, buttons on the picture) but share the same logic: onclick (). We use them in the same way. This idea is called polymorphism.
Polymorphism is based on the Greek word (usually) and the structure. We will create formats that can take many forms or objects that are effective.
Polymorphism with a function:
We create two classes: donkeys and dogs, both of which can make different sounds. Then we run both organizations and phones using the same way.
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)
Output:
Groarrr
woff woff!
Polymorphism with abstract Class:
To do this, we create a classmate called a document. This class does not have an application but it defines the format (format) in all forms.
When doing editing, you will not know the type of document it uses (user PDF or Word).
Will it be a bigger hurricane, than you would have found in 20 types of documents?
for document in documents:
print document.name + ': ' + document.show()
To do this, we create a classmate called a document. This class does not have an application but it defines the format (format) in all forms. If we specify the action (), then PdfDocument or WordDocument must have (). Full Code:
class Document:
def __init__(self, name):
self.name = name
def show(self):
raise NotImplementedError("Subclass must implement 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!
We have a unique score for the various types of substances (PDF, Word) follow the same pattern.
Polymorphism Example:
Another example would be the concrete structure of a car that has a unit () and a stop ().
We have built both motorcycles and cars, both for cars. The rules of what I would do would be:
class Car:
def drive abstract, no implementation.
def stop abstract, no implementation.
class Sportscar(Car):
def drive: implementation of sportscar
def stop: implementation of sportscar
class Truck(Car):
def drive: implementation of truck
def stop: implementation of truck
Then we can find the car that is called the call without considering whether it should be a sports car or car. Full Code:
class Car:
def __init__(self, name):
self.name = name
def drive(self):
raise NotImplementedError("Subclass must implement abstract method")
def stop(self):
raise NotImplementedError("Subclass must implement abstract method")
class Sportscar(Car):
def drive(self):
return 'Sportscar driving!'
def stop(self):
return 'Sportscar braking!'
class Truck(Car):
def drive(self):
return 'Truck driving slowly because heavily loaded.'
def stop(self):
return 'Truck braking!'
cars = [Truck('Bananatruck'),
Truck('Orangetruck'),
Sportscar('Z3')]
for car in cars:
print car.name + ': ' + car.drive()
Output:
Bananatruck: Truck driving slowly because heavily loaded.
Orangetruck: Truck driving slowly because heavily loaded.
Z3: Sportscar driving!
|
Comments
Post a Comment