Python Object-Class 4 inheritance and method overloading

Source: Internet
Author: User

1. Inheritance of classes and overloading of methods

The above is defined by a Class A, and then by the definition of a class B,b inherits the Class A, so that B has a non-private properties and methods.

1 classWasher:2company='ZBL'3def __init__ (self,water=Ten, scour=2):4Self._water=water #不想让用户直接访问实例变量, can be marked as private5Self.scour=Scour6Self.year= -#这是生产日期7 #属性包装, wraps the water property into a method that is actually accessed when the user uses water8 @staticmethod #定义一个静态方法9 def spins_ml (spins):Ten         returnspins*0.4 OnePrint'Company :', Washer.company) A#print ('Year :', Self.year) #错误, static methods cannot use instance properties - @classmethod - def get_washer (cls,water,scour): #cls相当于实例方法中的self, call is not provided with this parameter the         returnCLS (WATER,CLS.SPINS_ML (Scour)) #cls代表类名Washer, it is not hard-coded (the class name is not changed to CLS, if the CLS is replaced by the class name is also the same, but if the name of the change in this place also needs to change) -      - @property - def water1 (self): #如果用户使用 instance. Water is equivalent to accessing this method instead of actually accessing the property +         returnSelf._water -      + @water1. Setter A def water1 (self,water): at         if 0<water<= -: -Self._water=Water -         Else: -Print'Set failure! ') - @property - def total_year (self): in         return  .-Self.year -      to def set_water (self,water): +Self.water=Water -  the def set_scour (self,scour): *Self.scour=Scour $ Panax Notoginseng def add_water (self): -Print'Add Water:', Self._water) the  + def add_scour (self): APrint'Add Scour:', Self.scour) the  + def start_wash (self): - Self.add_water () $ Self.add_scour () $Print'Start Wash ...') -          - classWasherdry (Washer): # Create a new class that inherits from washer the def Dry (self): #新类中可以定义只属于子类的新方法 -Print'Dry cloths ...')Wuyi def start_wash (self): #在子类中新定义与父类同名的方法就是方法的重载 the Self.add_scour () - Self.add_water () Wu          - if__name__=='__main__': About# # Print (WASHER.SPINS_ML (8)) $# # W=Washer () -# # Print (W.SPINS_ML (8)) -# # W=washer ( $, WASHER.SPINS_ML (8)) - # # W.start_wash () Aw=Washerdry () + W.start_wash () the print (W.scour,w.company) -W.dry ()

Python Object-Class 4 inheritance and method overloading

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.