Python object-oriented-Class 4 inheritance and method overloading, python-4

Source: Internet
Author: User

Python object-oriented-Class 4 inheritance and method overloading, python-4

1. class inheritance and method Overloading

 

The above defines A class A, and then defines A Class B, B inherits Class A, so that B has the non-private attributes and methods of.

1 class runner Er: 2 company = 'zbl' 3 def _ init _ (self, water = 10, scour = 2): 4 self. _ water = water # Do not want users to directly access instance variables. It can be marked as private 5 self. scour = scour 6 self. year = 2000 # This is the production date 7 # property packaging. Wrap the water property into a method, when using water, the user actually accesses Method 8 @ staticmethod # defines a static method 9 def spins_ml (spins): 10 return spins * 0.411 print ('Company: ', cycler. company) 12 # print ('year: ', self. year) # error. The static method cannot use the instance attribute 13 @ classmethod14 def get_washer (cls, water, scour): # cls is equivalent The self in the instance method does not need to provide this parameter 15 return cls (water, cls. spins_ml (scour) # cls indicates the class name Washer, so it is not hard-coded (you do not need to change the class name to cls. If cls is replaced by the class name, however, if you change the class name, you also need to change it.) 16 17 @ property18 def water1 (self): # If you use an instance. water is equivalent to accessing this method, rather than actually accessing attribute 19 return self. _ water20 21 @ water1.setter22 def water1 (self, water): 23 if 0 <water <= 500: 24 self. _ water = water25 else: 26 print ('set Failure! ') 27 @ property28 def total_year (self): 29 return 2017-self.year30 31 def set_water (self, water): 32 self. water = water 33 34 def set_scour (self, scour): 35 self. scour = scour 36 37 def add_water (self): 38 print ('add water: ', self. _ water) 39 40 def add_scour (self): 41 print ('add scour: ', self. scour) 42 43 def start_wash (self): 44 self. add_water () 45 self. add_scour () 46 print ('start wash... ') 47 48 class WasherDry (handler ER): # create a new class that inherits from Washer49 def dry (self ): # The New Method 50 print ('dry cloths... ') 51 def start_wash (self): # The method that defines the same name as the parent class in the subclass is the method overload 52 self. add_scour () 53 self. add_water () 54 55 if _ name __= = '_ main _': 56 # print (author er. spins_ml (8) 57 ## w = Washer () 58 ## print (w. spins_ml (8) 59 ## w = faster Er (200, faster er. spins_ml (8) 60 # w. start_wash () 61 w = WasherDry () 62 w. start_wash () 63 print (w. scour, w. company) 64 w. dry ()

 

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.