Object Oriented--Polymorphism

Keywords: Python Programming Java

1. Overview * Overview * Overview * Overview * Overview * Overview * Overview * Overview * Overview * Overview * Overview * Overview * Overview * Overview * Overview * Overview * Overview * Overview

Polymorphism is an important feature of object-oriented. Simply put, "one interface, multiple implementations", it means that different subclasses are derived from a basic class, and each subclass inherits the same method name while implementing different methods of the parent class. This is the multiple forms of the same thing.

Programming is actually a process of abstracting the concrete world. Polymorphism is an embodiment of abstraction. It abstracts the common points of a series of concrete things. Through this abstract thing, we can talk with different concrete things and send the same message to different kinds of objects.  

Polymorphism allows objects of a subclass to be used as objects of a parent type, and a reference to an object of a parent type points to an object of its subtype, and the method invoked is the method of that subtype. The code that references and invokes methods here is determined before compilation, and the object that references refer to can be dynamically bound during runtime.

2. Polymorphism.

An abstract class has many subclasses, that is, many forms. Polymorphism depends on inheritance, and there is no polymorphism without inheritance.

 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 = breed
14     pass
15 
16 
17 class Cat(Animal):
18 
19     def __init__(self, name, age, breed):
20         super(Cat, self).__init__(name, age)
21         self.breed = breed
22     pass

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

Polymorphism

Polymorphism means that functions with different functions can use the same function name, so that functions with different contents can be called with one function name. In object-oriented methods, polymorphism is generally expressed as sending the same message to different objects, and different objects will produce different behaviors (i.e. methods) when receiving it. That is to say, each object can respond to a common message in its own way. The so-called message is to call a function, different behavior means different implementations, that is, to execute 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 = score
15 
16     def identity(self):
17         return 'I am a Student, my name is %s' % self.name
18 
19 
20 class Teacher(Person):
21     def __init__(self, name, age, gender, course):
22         super(Teacher, self).__init__(name, age, gender)
23         self.course = course
24 
25     def identity(self):
26         return 'I am a Teacher, my name is %s' % self.name
27 
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))
#Operation results

I am a Person, my name is Tim
I am a Student, my name is bigberg
I am a Teacher, my name is Luodao

Method calls will work on the actual type of x. s is a Student type. It actually has its own identity() method and identity() method inherited from Person, but calling s. identity() always finds its own definition first, and if not, it looks up the inheritance chain until it is found in a parent class.

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

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. Dynamic language calls instance methods without checking the type. As long as the method exists and the parameters are correct, it can be called.

Posted by djtozz on Mon, 10 Jun 2019 11:11:04 -0700