Python object-oriented _ details on class inheritance and method overloading, python object-oriented

Source: Internet
Author: User

Python object-oriented _ details on class inheritance and method overloading, python object-oriented

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.

Class runner Er: company = 'zbl' def _ init _ (self, water = 10, scour = 2): self. _ water = water # Do not want users to directly access the instance variables. It can be marked as private self. scour = scour self. year = 2000 # This is the production date # property packaging. The water property is packaged as a method. When you use water, the actual access method @ staticmethod # defines a static method def spins_ml (spins ): return spins * 0.4 print ('Company: ', explorer. company) # print ('year: ', self. year) # error. The static method cannot use the instance attribute @ classmethod def get_washer (cls, water, scour): # cls is equivalent to self in the instance method. This parameter retur is not required for calling. N 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.) @ property def water1 (self): # If you use an instance. water is equivalent to accessing this method, rather than actually accessing the property return self. _ water @ water1.setter def water1 (self, water): if 0 <water <= 500: self. _ water = water else: print ('set Failure! ') @ Property def total_year (self): return 2017-self.year def set_water (self, water): self. water = water def set_scour (self, scour): self. scour = scour def add_water (self): print ('add water: ', self. _ water) def add_scour (self): print ('add scour: ', self. scour) def start_wash (self): self. add_water () self. add_scour () print ('start wash... ') class WasherDry (handler ER): # create a new class that inherits from handler er def dry (self ): # The New Method print ('dry cloths... ') def start_wash (self): # The method that defines the same name as the parent class in the subclass is the method overload self. add_scour () self. add_water () if _ name __= = '_ main _': # print (author er. spins_ml (8) # w = Washer () # print (w. spins_ml (8) # w = faster Er (200, faster er. spins_ml (8) # w. start_wash () w = WasherDry () w. start_wash () print (w. scour, w. company) w. dry ()

The above python object-oriented _ detailed introduction of class inheritance and method Overloading is all the content shared by the small Editor. I hope to give you a reference, and I hope you can provide more support for the customer's house.

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.