python basis - the Object-oriented inheritance

# Inheritance is a way to create a new class, the new class can inherit one or more parent classes, 
# parent class can be called a base class or super class, the new class can be called a derived class, subclass of 
class ParentClass1 :
     # defined parent class. 1 
    Pass 
class ParentClass2:
     # defined parent class 2 
    Pass 
class SubClass1 (ParentClass1):
     # inherit a parent 
    Pass 
class SubClass2 (ParentClass1, ParentClass2):
     # inherits two parent 
    Pass 
Print (SubClass1. __bases__ ) # View parent 
Print (SubClass2. __bases__ )
 class Animal:
     DEF EAT (Self):
         Print( " % S IS eating " % the self.name)
     DEF Drink (Self):
         Print ( " % S IS Drinking " % the self.name)
     DEF shit (Self):
         Print ( " % S IS shiting " % the self.name)
     DEF PEE (Self):
         Print ( " % S iS peeing " % the self.name)
 # subclass can use the function attributes and data of properties of the parent 
class Cat (Animal):
     DEF  the __init__ (Self, name): 
        the self.name =name 
        self.breed = " cat " 
    DEF Cry (Self):
         Print ( " meow " )
 class Dog (Animal):
     DEF  __init__ (Self, name): 
        self.name = name 
        self.breed = " dog " 
    DEF Cry (Self):
         Print ( " bark " ) 

c1 = Cat ( " white house black Cat " ) 
c1.eat () 
# the current class if not eat attribute, it will go looking for parent class 

c2 = Cat (" Black or white house " ) 
c2.drink () 

d1 = Dog ( " fat family dogs " ) 
d1.eat () 
# if the child class and parent class have the same attributes, will give priority to the use of sub-categories the property (unless otherwise stated) 
# Therefore, we in the development process, if you define a class a, and then want to re-establish a class B, and 
# many functions and a B are the same, we just need to a succession over, and then write about different functions in B 
# it 
# attention when there are inherited, must pay attention to find the property 
class F:
     DEF f1 (Self):
         Print ( " an 1 " )
     DEF F2 (Self):
         Print ( ' F.f2 ' ) 
        self.f1 () 
classS (F):
     DEF f1 (Self):
         Print ( " S.f1 " ) 
S = S () 
s.f2 () 
# We look at the code execution process 
# S as an object - to execute the method f2 - his is not a class - to find the parent - the parent class have - 
# attention; parent class is this self s-- print F.f2-- then perform f1, - first in his class to find - 
# find - print S.f1 
# If we use the method with the parent class in the subclass, the two approaches 
# a, namely the direct use of the class name: father name method name () 
# two, super ( ) 
class Animal:
     DEF  the __init__ (Self, name, Age, Gender): 
        the self.name = name 
        self.age = = Age
        self.gender Gender 

class people (Animal):
     DEF  the __init__ (Self, name, Age, Gender): 
        Animal. the __init__ (Self, name, Age, Gender)
 class Cat (Animal):
     DEF  the __init__ (Self, name, Age, Gender ):
         # this is not the incoming object itself, 
        # Super () is equivalent to Super (Cat, Self) 
        Super (). __init__ (name, Age, Gender) 
PEO = people ( ' wangcong ' , 21, ' Mal ' ) 
CAT Cat = ( ' CAT ' , 2, ' MALE ' )
Print (peo.name, cat.name) # wangcong CAT 
# concluded that the relevant portfolio and inheritance, 
# 1, who is an animal, a cat is an animal, that's inheritance, 
# 2, school classrooms, students have the books. This is a combination of relationship, 
#        a class in another class, an object has a relationship with another object 
# simple look at the combination of 
List1 = list () # We said before the list is a class used to instantiate an object 
str1 = " the Hello "  # same str1 is a string object 
list1.append (str1)
 Print (List1)
 # At this point, the relationship between them is a combination of

 

Guess you like

Origin www.cnblogs.com/cong12586/p/11366370.html