In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "polymorphism and abstract example analysis of python". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "Python polymorphism and abstract example analysis"!
polymorphism
In object-oriented programming, polymorphism refers to multiple forms of a data type.
Examples of classes:
class Animal:kind = 'Animal'def __init__(self, name): self.name = namedef eat(self):print(f'{self.name} Eating! ')class Cat(Animal):def __init__(self, name, count=0):self.count = count Animal.__ init__(self, name)def work(self):print(f'cat "{self.name}" caught {self.count} mouse') class Dog(Animal):def __init__(self, name, count=0):self.count = count Animal.__ init__(self, name)def work(self):print(f'Hound dog "{self.name}" caught {self.count} rabbits')
The above case type Animal class has two forms of data, one is cat and the other is dog.
The advantage of polymorphism is that it can unify the interface and improve the efficiency of working with many people on large projects.
Examples of unified interfaces:
def work(x, num):x.count += num x.work ()white_cat = Cat ('white cat') balck_dog = Dog ('black old') work(white_cat, 2)work(balck_dog, 3)out: Cat "white cat" caught 2 mice Dog "black old') caught 3 rabbits
Explanation:
In the multi-person cooperation project: A is the project planner, he defines the Animal class; B defines the Cat class, C defines the Dog class, then D instantiates Cat and Dog in the code, to write the code about Cat and Dog work, if there is no unified interface work function, then it will be very troublesome to write, and it is necessary to judge the type for different instances and then deal with them separately. At this point, you can use the concept of polymorphism, A to write a unified interface, so that B, C, and D can use the unified interface work function when writing Cat and Dog instances. This improves teamwork, reduces code duplication, and improves code readability.
abstract
Abstract in object-oriented programming refers to a special class, abstract class is not used to implement functionality, but to standardize the subclass method name.
Examples of classes:
class WorkAnimal:def work(self):raise NotImplementedError ('Animals that want to work must implement the work method uniformly! ')
Explanation:
You can see that WorkAnimal itself has no implementation function, but is used to prompt all programmers who write subclasses to unify the method names of subclasses. Continuing with the above example, assuming that programmer C did not follow the abstract class naming convention when writing the Dog class, his code is as follows:
class Dog(Animal, WorkAnimal):def __init__(self, name, count=0):self.count = count Animal.__ init__(self, name)def catch(self):print(f'Hound "{self.name}" caught {self.count} rabbits')
At this point programmers C and D call the unified interface work function will report an error.
The complete code is as follows:
class Animal:kind = 'Animal'def __init__(self, name): self.name = namedef eat(self):print(f'{self.name} Eating! ')class WorkAnimal:def work(self):raise NotImplementedError (') class WorkAnimal: def work (self): raise NotImplementedError (') ')def work(x, num):x.count += num x.work()class Dog(Animal, WorkAnimal):def __init__(self, name, count=0):self.count = count Animal.__ init_(self, name)def catch(self):print(f'dog "{self.name}" caught {self.count} rabbits') balck_dog = Dog ('old black dog') work(balck_dog, 3)out:NotImplementedError: Animals that want to work must implement the work method uniformly! At this point, I believe that everyone has a deeper understanding of "python polymorphism and abstract example analysis", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.