#!/usr/bin/env python
#-*-Coding:utf-8-*-
# Author:changhua Gong
class Person (object):
def __init__ (self, name):
Self.name = name # static property
Def say (self): # method, dynamic property
Print ("Say ...")
def speak (self):
Print ("Speak ...")
Class Relationship:
def mk_friends (Self, somebody):
Print ("%s is making friends with%s."% (Self.name, somebody.name))
"Here somebody (Class) as a parameter, to ensure that a person renaming is still a new name, because the properties of the modified instance is to modify the corresponding memory data"
Self.friends.append (Somebody) # Add a friend to your friends list and don't consider repeating your friend question
Somebody.friends.append (self) # Add a friend to the other's friends list
‘‘‘
When multiple inheritance is instantiated, the constructor of the subclass is first looked for, and if none is in the left-to-right order, the construction method of the parent class is searched,
After finding the first construction method, we no longer look for the constructor of the parent class, and it is recommended to use Super key to inherit the parent class construct when multiple inheritance occurs.
Class E:
#经典类
Pass
Class E1 (object):
#新式类
Pass
There is the following inheritance: Class A Inherits object, Class B, C inherits A, Class D inherits B, C
In Py2, the classical class inherits by the depth first, and the new class inherits by the breadth first;
In Py3, both classic and modern classes inherit by breadth precedence.
The default is the classic class in Py2, and only the explicit inheritance of object is the new class
Py3 is the new class by default and does not have to inherit object explicitly?
‘‘‘
Class man (relationship, person):
def __init__ (self, Name, age): # This overrides the constructor method, and inherits the constructor of the parent class without overriding
#person. __init__ (self, name) # Inherit the construction method of the parent class
Super (man, self). __init__ (name) # inherits the constructor of the parent class, another way of writing, paying attention to the position of self, which is recommended when multiple inheritance
Self.age = Age
Self.friends = []
Def say (self):
Person.say (self) # The method that inherits the parent class, of course, can be commented out, does not inherit the method of the parent class
Print ("Say hello...%s"% (self.name))
Print ("Say hello...%s"% (self.age))
P1 = Man ("Daidai", 23)
P1.name = "New Boy"
P1.say ()
P2 = Man ("Xiongxiong", 18)
P2.name = "New Girl"
P1.mk_friends (p2)
Print ("Daidai's friend is%s."% P1.friends[0].name)
Print ("Xiongxoing's friend is%s."% P2.friends[0].name)
This article is from the "90SirDB" blog, be sure to keep this source http://90sirdb.blog.51cto.com/8713279/1921756
Python Multiple Inheritance