Object-oriented: Polymorphism and object-oriented Polymorphism

Source: Internet
Author: User

Object-oriented: Polymorphism and object-oriented Polymorphism

I. Overview

Polymorphism is an important feature of object orientation. Simply put, "one interface and multiple implementations" means that different subclasses are generated in a base class, each subclass inherits the same method name and implements different methods of the parent class. This is the form of the same thing.

Programming is actually a process of abstracting the specific world. polymorphism is an embodiment of abstraction. It abstracts the commonalities of a series of specific things, and then through this abstract things, dialog with different things,Sending the same message to different classes of objects will have different behaviors..

Polymorphism allows the subclass object to be used as the parent class object. A reference of a parent class points to its child type object, and the method called is the child type method. The reference and call method code are determined before compilation, and the referenced object can be dynamically bound during running.

2. Polymorphism

An abstract class has multiple subclasses, which are in multiple forms. Polymorphism depends on inheritance. If there is no inheritance, there is no polymorphism.

 1 class Animal(object): 2  3     def __init__(self, name, age): 4         self.name = name 5         self.age = age 6     pass 7  8  9 class Dog(Animal):10 11     def __init__(self, name, age, breed):12         super(Dog, self).__init__(name, age)13         self.breed = breed14     pass15 16 17 class Cat(Animal):18 19     def __init__(self, name, age, breed):20         super(Cat, self).__init__(name, age)21         self.breed = breed22     pass
View Code

We have defined an Animal class and its two subclasses: Dog and Cat. These two subclasses represent the various forms of the Animal class.

Iii. Polymorphism

Polymorphism means that functions with different functions can use the same function name, so that you can use a function name to call functions with different content. In object-oriented methods, polymorphism is generally expressed as follows: send the same message to different objects, and different objects will produce different behaviors (that is, methods) during receiving ). That is to say, each object can respond to common messages in its own way. A message is called a function. Different actions refer to different implementations, that is, executing different functions.

 1 class Person(object): 2     def __init__(self, name, age, gender): 3         self.name = name 4         self.age = age 5         self.gender = gender 6  7     def identity(self): 8         return 'I am a Person, my name is %s' % self.name 9 10 11 class Student(Person):12     def __init__(self, name, age, gender, score):13         super(Student, self).__init__(name, age, gender)14         self.score = score15 16     def identity(self):17         return 'I am a Student, my name is %s' % self.name18 19 20 class Teacher(Person):21     def __init__(self, name, age, gender, course):22         super(Teacher, self).__init__(name, age, gender)23         self.course = course24 25     def identity(self):26         return 'I am a Teacher, my name is %s' % self.name27 28 29 def identical(x):30     return x.identity()31 32 p = Person('Tim', 22, 'FM')33 s = Student('bigberg', 22, 'M', 88)34 t = Teacher('Luodao', 44, 'M', 'python')35 36 print(identical(p))37 print(identical(s))38 print(identical(t))
View Code
# Running result I am a Person, my name is TimI am a Student, my name is bigbergI am a Teacher, my name is Luodao

The method call will apply to the actual type of x. S is the Student type. It actually has its own identity () method and the identity () method inherited from Person, but calls s. identify () always searches for its own definition first. If there is no definition, it searches up along the inheritance chain until it is found in a parent class.

Because Python is a dynamic language, the parameter x passed to the function identical (x) is not necessarily a child type of Person or Person. Any data type instance can be used as long as it has an identity () method:

Class Book (object): def identity (self): return 'I am a book.' print (identical (B) # result I am a book

 

This is one of the biggest differences between dynamic and static languages (such as Java. The Dynamic Language calls the instance method without checking the type. If the method exists and the parameter is correct, the method can be called.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.