Package
Class to hide certain properties and methods, or defined as a private, internal use only class can not be accessed outside the class, or leave a small number of interfaces (functions) for external access; from an article in the private attributes and private code in the method captures the characteristic.
man class (): DEF the __init __ (Self, name): the self.name name = Self .__ the passwd = "123456" DEF changepass (Self, newpasswd): Self .__ the passwd = newpasswd Print (Self .__ the passwd) Liming = man ( "Liming ") # no liming.passwd / liming .__ passwd property # can only be viewed or modified by internal attributes about changepass method liming.changepass (" abc123456 ")
inherit
A kind of inside, there is a total of properties and actions. Such as animal, there are common attributes (length, height, etc.) and actions (eating, running, call, etc.). For reuse in the program code, the code does not need to repeat the same preparation. Defined in the parent class properties, methods automatically inherited by subclasses.
For example, the following example (inherited only):
Dog class: DEF __init __ (Self, name): self.name name = DEF EAT (Self): Print ( "% S, eating ..."% self.name) DEF RUN (Self): Print ( "% S , I was running ... "% self.name) class Poodle (dog): #poodle (Poodle) is a subclass of .poodle dog class inherits all the attributes and methods of dog class. Pass my_dog = Poodle ( "flowers") my_dog.eat () my_dog.run () # Output flowers, eating ... flowers, was running ...
Of course, not only subclasses can inherit the parent class, you can also add other methods and properties on the basis of the parent class.
Dog class: DEF __init __ (Self, name): self.name name = DEF EAT (Self): Print ( "% S, eating ..."% self.name) DEF RUN (Self): Print ( "% S , I was running ... "% self.name) class Poodle (Dog): DEF Lovely (Self): # adding new methods ! Print ("% S iS Lovely "% self.name) my_dog = Poodle (" flowers " ) my_dog.eat () my_dog.run () my_dog.lovely () # output flowers, eating ... flowers, was running ... flowers is lovely!
Reconstruction of the parent process (override)
Override inherited methods:
Dog class: DEF __init __ (Self, name): self.name name = DEF EAT (Self): Print ( "% S, eating ..."% self.name) DEF RUN (Self): Print ( "% S , was running ... "% self.name) class Poodle (Dog): DEF rUN (Self): ! Print ("% S iS running "% self.name) my_dog = Poodle (" flowers ") my_dog.eat ( ) my_dog.run () # output flowers, eating ... floret is running!
First parent class code execution method then execute the code sub-class method:
Dog class: DEF __init __ (Self, name): self.name name = DEF EAT (Self): Print ( "% S, eating ..."% self.name) DEF RUN (Self): Print ( "% S , was running ... "% self.name) class Poodle (Dog): DEF rUN (Self): dog.run (Self) ! Print ("% S iS running "% self.name) my_dog = Poodle (" flowers ") my_dog.eat () my_dog.run () # output flowers, eating ... flowers, was running ... floret is running!
Perfect Reconstruction constructor:
class dog: def __init__(self,name): self.name = name def eat(self): print("%s,正在吃..."% self.name) def run(self): print("%s,正在跑..."% self.name) class poodle(dog): def lovely(self): print("%s is lovely!" % self.name) class jiwawa(dog): def __init__(self,name,gun): dog.__init__(self,name) self.gun = gun def wawa(self): print("%s 是吉娃娃狗种!" % self.name) def xgun(self): print("%s" % self.gun) dog1 = poodle("小花") dog1.eat() dog1.run() dog2 = jiwawa("小黑","yes") dog2.wawa() dog2.xgun () flowers, was running ... ## outputs flowers, eating ... black Chihuahua dog breeds! yes
You can also use this wording
super(jiwawa,self).__init__(name)
The new class of Classic
Classic is the old version, the new class is the new version.
# Classic class Dog: # new-style class class dog (object):
# Classic class wording Dog .__ the init __ (Self, name) # new-style class writing uper (jiwawa, self) .__ init __ (name)
The difference between Classic and new classes: multiple inheritance.
Multiple Inheritance
class dog: def __init__(self,name): self.name = name def eat(self): print("%s,正在吃..."% self.name) def run(self): print("%s,正在跑..."% self.name) class Relation(object): def makefriends(self,obj): print("%s要和%s交朋友!" % (self.name,obj.name)) class poodle(dog,Relation): def lovely(self): print("%s is lovely!" % self.name) class jiwawa(dog): def __init__(self,name,gun): #dog.__init__(self,name) super(jiwawa,self).__init__(name) self.gun = gun def wawa(self): print("%s 是吉娃娃狗种!" % self.name) def xgun(self): print("%s" % self.gun) dog2 = jiwawa ( "black", "yes") DOG1 = Poodle ( "flowers") dog1.makefriends (dog2) # Output flowers and black to make friends!
Polymorphism