1 classParent class:2 defDrinking (self):3 Print("I'm Lao, I love drinking.")4 defeat Chicken (self):5 Print("I'm Lao, I'm eating chicken.")6 defplay football (self):7 Print("I'm Lao, I have to play football today")8 9 Ten classChild Class (parent class): One defBig Health Care (self): A Print('I'm a son, I want a big health care') - defDrinking (self): - Print("I'm a son and my son can't drink.") the defeat Chicken (self): -Super (subclass, self). Eat chicken ()#to execute a method in a parent class - Print("I'm a son, I don't want to eat chicken. I hate eating chickens .") - #parent class. Eat chicken (self) call directly + - Print("call a method in a subclass:") +A=sub-class () A A. Major health care () at Print("calling methods in the parent class") - A. Play football () - Print("I'm not satisfied with the method in the parent class, I want to override the method in the parent class") - A. Drinking () - Print("Although I rewrote the contents of the parent class, I still want to call the parent class method") -A. Eat chicken ()
Operation Result:
" D:\Program Files (x86) \python36\python.exe " F:/python from getting started to giving up/7.20/ Object-oriented 2.py calling methods in subclasses: I'm a son, I want a big health care call method in the parent class I'm Lao Tzu, I have to play football today I am not satisfied with the methods in the parent class, I am a son, my son cannot drink. Although I rewrote the contents of the parent class, I still want to call the parent class. I'm Lao, I'm a son. I don't want to eat chicken. I hate to eat chicken. Process finished with exit code 0
You can execute both the parent class and the method of the subclass.
1 #!/usr/bin/env Python32 #-*-coding:utf-8-*-3 " "4 Administrator5 2018/7/206 " "7 #scenario, suppose that there is a Web application, developers need to give the program, the world new features. Add new function modules for the modules that others have already written8 classRequestHandler:9 def __init__(self):Ten Pass One A defGet (self): - Print('This is the complete framework') - the classbaserequest (requesthandler): - def __init__(self): - Pass - defGet (self): + Print('This is a custom added feature') - Super (Baserequest, self). Get () +D=baserequest () AD.get ()
Operation Result:
" D:\Program Files (x86) \python36\python.exe " F:/python from getting started to discarding/7.20/ object-oriented 2.py This is the custom added feature which is the complete framework of process finished with exit code 0
1 #!/usr/bin/env Python32 #-*-coding:utf-8-*-3 " "4 Administrator5 2018/7/206 " "7 #application scenarios, adding new function modules to the modules that others have already written8 classRequestHandler:9 def __init__(self):Ten Pass One A defGet (self): - Print('This is the complete framework') - the classbaserequest (requesthandler): - def __init__(self): - Pass - defGet (self): + Print('This is a custom added feature') - Super (Baserequest, self). Get () +D=baserequest () A D.get () at - - #class Father: - #def drink (self): - #print ("Drink") - #def Eat Chicken (self): in #Print ("I'm eating chicken") - # to # + #class Son (Father): - #def Big Health (self): the #print (' I want big Health care ') * #def Eat Chicken (self): $ ## Super (son,self). Eat chicken () #执行父类中的方法Panax Notoginseng #Print ("I'm not eating chickens.") I hate to eat chicken ") - #Father. Eating Chicken (self) the # + # A #A=son () the # + #A. Eat chicken () - $ $ - #str () - #class Bar: the #def __init__ (self,name,age,gender): - #" "Wuyi #constructor method, the class name plus attribute automatically executes the constructor method the #:p Aram Name: - #:p Aram Age: Wu #:p Aram Gender: - #" " About #Self.name=name $ #Self.age=age - #Self.gender=gender - #def foo (self): - #print (Self.name,self.age,self.gender, "Chifan") A # + #Z1=bar ("Aliex", "Nan") the #print (z1.name) - #Z1.foo () $ #z1.name= ' Tom ' the #z1.age=12 the #z1.gender= ' man ' the #Z1.foo (' Mountain chopping wood ') the #Z1.foo (' Sea-catching shrimp ') - in the the About #class Bar: the #def foo (self,arg): the #print (Self,self.name,self.age,self.gender,arg) the # + # - #Z1=bar () the #z1.name= ' Tom 'Bayi #z1.age=12 the #z1.gender= ' man ' the #print (z1) - #Z1.foo (' a ') - #print (' Ceshi '. Center (+, ' * ')) the # the #Z2=bar () the #print (z2) the #z2.foo (' 66666 ')Practice Drafts
Python entry 22nd Day-object oriented