Python object-oriented example explained

Source: Internet
Author: User

Or with the last washing machine example:

 1 class washer:2 company= ' ZBL ' 3 def __init__ (self,water=10,scour=2): 4 self._water=water #不想让用户直接访问实例变量     , can be marked as Private 5 Self.scour=scour 6 self.year=2000# This is the production date 7 #属性包装, the water attribute is wrapped into a method, the user is actually accessing the method when using water 8 @staticmethod #定义一个静态方法 9 def spins_ml (spins): Return spins*0.411 print (' Company: ', Washer.company) 1 2 #print (' Year: ', self.year) #错误, static methods cannot use instance Properties @property15 def water1 (self): #如果用户使用 instance. Water equivalent to accessing this party method, instead of the true access attribute, return Self._water17 @water1. Setter19 def water1 (self,water): If 0<water <=500:21 self._water=water22 else:23 print (' Set failure!         ') @property25 def total_year (self): return 2017-self.year27 def set_water (self,water): 29 Self.water=water def set_scour (self,scour): Self.scour=scour, Def Add_wa ter (self): print (' Add water: ', Self._water) 36Notoginseng def add_scour (self): + print (' Add scour: ', Self.scour) Max def start_wash (self): Self.add_wat ER () self.add_scour () print (' Start wash ... ') if __name__== ' __main__ ': print (washer. SPINS_ML (8)) W=washer () print (W.SPINS_ML (8)) W=washer (200,WASHER.SPINS_ML (8)) W.start_wash ()

Output Result:

The following class methods are spoken:

 1 class washer:2 company= ' ZBL ' 3 def __init__ (self,water=10,scour=2): 4 self._water=water #不想让用户直接访问实例变量     , can be marked as Private 5 Self.scour=scour 6 self.year=2000# This is the production date 7 #属性包装, the water attribute is wrapped into a method, the user is actually accessing the method when using water 8 @staticmethod #定义一个静态方法 9 def spins_ml (spins): Return spins*0.411 print (' Company: ', Washer.company) 1 2 #print (' Year: ', self.year) #错误, static methods cannot use instance Properties @classmethod14 def get_washer (cls,water,scour): #cls相当于实例方法中的 Self, the call is not to provide this parameter to the WATER,CLS.SPINS_ML (scour) #cls代表类名Washer, so it is not hard-coded (the class name is not modified CLS, if the CLS is replaced by the class name is also correct, But if the change of name this place also need to change) @property18 def water1 (self): #如果用户使用 instance. Water is equivalent to accessing this method instead of actually accessing the property, and return Self._w Ater20 @water1. Setter22 def water1 (self,water): if 0<water<=500:24 self._water= Water25 else:26 print (' Set failure! ') @property28 def total_year (self): return 2017-SELF.YEAR30 to Def Set_wateR (Self,water): Self.water=water set_scour (self,scour): Self.scour=scour 36 Notoginseng def add_water (self): Water print (' Add: ', Self._water) Max def add_scour (self): print (' Add Scour: ', Self.scour '), Start_wash (self): Self.add_water () Self.add_scour (' Start wash ... ') if __name__== ' __main__ ': # # print (WASHER.SPINS_ML (8)) # # W=washer () # # # print (     W.SPINS_ML (8)) # # W=washer (200,WASHER.SPINS_ML (8)) # # # W.start_wash () w=washer.get_washer (100,9) #类名. Class method Name 55 W.start_wash ()

-----------------------

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.