In the previous blog simply said about the inheritance of the class syntax, today in the specific class of related knowledge.
First, use a piece of code to review the inheritance of classes you've learned before:
Class Parent: def hello (self): print ("Calling method of Parent class ...") class child (Parent): # pass def hello (self): print ("Method of calling Subclass ...") p=parent () P.hello () C=child () C.hello ()
There are several issues to be aware of in the inheritance of classes:
(1) If a method or property with the same name as the parent class is defined in the subclass, the method or property corresponding to the parent class is automatically overwritten.
Import random as Rclass Fish: def __init__ (self): self.x=r.randint (0,10) self.y=r.randint (0,10) def Move (self): self.x-=1 Print ("My location is:", Self.x,self.y) class Goldfish (fish): passclass Carp (fish) : Passclass Salmon (fish): passclass Shark (fish): def __init__ (self): #调用未绑定的父类方法, passing in the instantiated object of a subclass # Fish.__init__ (self) #使用super () function super (). __init__ () self.hungry=true def Eat (self): if Self.hungry: print ("Foodie's dream is to eat every day") self.hungry=false Else: print ("Too much to Eat") Shark=shark () Shark.move () #这样写的话程序会报错 because the subclass shark overrides the #__init__ method, overwriting the __init__ method of the parent class
(2) Python also supports multiple inheritance
Class Base1: def foo1 (self): print ("I'm foo1, I'm a Base1 endorsement ...") class Base2: def foo2 (self): print (" I am foo2, I speak for Base2 ... ") class C (BASE1,BASE2): passc=c () c.foo1 () C.foo2 ()
(3) We can use a combination when there are no direct inheritance relationships between several classes.
For example, in the code below, turtle, Fish, pool three classes, they do not have a significant inheritance relationship, but the use of the combination can be very convenient to solve the problem between the several classes. The following code solves the problem of how many goldfish and turtles are in the output pond. The main solution is to put the turtle and the gold fish into the class of the pond.
Class Turtle: def __init__ (self,x): self.num=xclass Fish: def __init__ (self,x): self.num=xclass Pool: def __init__ (self,x,y): #将类的实例化放到另一个类中 self.turtle=turtle (x) self.fish=fish (y) def print_num (self): print ("There are a total of turtles%d in the pool, little fish%d!") "% (Self.turtle.num,self.fish.num)) Pool=pool (1,10) Pool.print_num ()
Finally, add some built-in functions about the class:
#前面是子类, followed by the base class #issubclass (Class,classinfo) #判断一个类是否为另一个类的子类 # A class is considered to be its own subclass class A:passclass B (A): Passprint (Issubclass (B,a)) #isinstance (Object,classinfo) #前面是一个实例化对象, followed by the class # check if the instance object belongs to this class b1=b ();p rint (Isinstance (b1,b)) #hasattr (Object,name) #测试一个对象是否有特定的属性class c:def __init__ (self,x=0): self.x=x# Be sure to note that Python does not have a variable definition c1=c () print (Hasattr (c1, "X")) #getat TR (Object,name[,default]) #返回对象指定的属性值, if not present, prints the default value of print (GetAttr (c1, ' X ')) print (GetAttr (c1, ' Y ', "The parameter you accessed does not exist") # SetAttr (Object,name,value) #设定属性的值, if not present, will automatically create a new SetAttr (C1, "Y", "Yaoxiangxi") print (GetAttr (c1, ' Y ', "The parameter you are accessing does not exist") # Delattr (Object,name) #删除属性delattr (C1, ' Y ') #property (fget=none,fset=none,fdel=none,doc=none) #通过属性设置属性class c:def __ Init__ (self,size=10): Self.size=size def getsize (self): return self.size def setSize (Self,value) : Self.size=value def delsize (self): del self.size #删除某个属性 #便于代码的修改 x=property (Getsize,setsi ze,delsize) C1=c () print (C1.getsize ()) print (c1.x) C1.x=100print (C1. x)
The final property will be explained in detail in the descriptor's blog.
The above is the Python 0 basic Primer 14 inheritance content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!