Introduction to how constructors are used

Source: Internet
Author: User
A class is an abstract concept that describes the definition of a thing.
An instance refers to a specific individual of that kind of thing, something that is specific

To make an analogy:
"Man" is a class. "Zhang San" is a concrete example of human beings.

The same is true when programming, you define a "class" yourself, and when you need it, use the definition of "class" to create a concrete example.

Creating an instance with the definition of a class is called instantiation of the class.
The instantiation process is called the constructor of the class to complete the data allocation for that instance

Here is an example of a washing machine to illustrate:

This is a simple manual washer W instantiates the washer class:

1 #class washer:2 class Washer: #定义一个Washer类 3  4     def __init__ (self): #这是定义类的构造函数, also a special instance method 5         self.water=0   #实例的属性 (variable) 6         self.scour=0 7  8     def add_water (self,water): #实例的方法 (function), you can accept the arguments passed after instantiation to the class instance variable, And because the instance inherits the properties and methods of the class, 9     #相当于传递给了类的实例化对象的实例变量10         print (' Add water: ', water) one by one         self.water=water# Reference an instance variable within a method of a class with self. Property name     Add_scour (self,scour):         self.scour=scour15         print (' Add scour: ', Scour)     def start_wash (self):         print (' Start wash ... ')         if __name__== ' __main__ ':     w= Washer () #类的实例化22     W.add_water (Ten) #传递参数到实例变量23     w.add_scour (2)     W.start_wash ()

Execution Result:

The Define instance method within a class requires manual addition of the self parameter, such as line 4th, but does not have to be manually added when the method is called after the class is instantiated, and Python automatically adds, for example, line 22nd.

The instantiation method is W=washer (), which shows that you need to add a parenthesis after the class.

An instance method that is defined within the class call class can be an instance name. Method Name (), for example

W.add_scour (2), add parameters when necessary.

The following changes the program, into a semi-automatic washing machine, that is, inside the class, the method calls each other.

washa.py:

1 class Washer:2  3     def __init__ (self): 4         self.water=0 5         self.scour=0 6  7     def set_water (self, Water): 8         self.water=water 9         self.add_water ()     def set_scour (self,scour):         self.scour=scour13         Self.add_scour () #在类内调用函数, with self. Method name     : Add_water (self),         print (' Add water: ', Self.water) 17 18     def add_scour (self):         print (' Add scour: ', Self.scour)-     def start_wash (self):         print (' Start wash ... ')         if __name__== ' __main__ ':     w=washer ()     W.set_water (     2)     W.start_wash () 29

can also be changed to:

1 class Washer:2  3     def __init__ (self): 4         self.water=0 5         self.scour=0 6  7     def set_water (self, Water): 8         self.water=water         9     def set_scour (self,scour): All         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 (         ' Start Wash ... ')         if __name__== ' __main__ ':     w=washer ()     W.set_water (     2) 28     W.start_wash ()

Operation Result:

To do the following changes, the user can not set the washing machine add water and detergent amount can also wash clothes:

1 class Washer:2  3     def __init__ (self): 4         self.water=10# Modify instance properties to the default standard laundry program required amount 5         self.scour=2 6  7     def set_water (self,water): 8         self.water=water         9     def set_scour (self,scour): one         self.scour= Scour     def add_water (self): +         print (' Add water: ', self.water)     def add_scour (self): 17         print (' Add scour: ', Self.scour)-     def start_wash (self):         self.add_water ()         self.add_ Scour ()         print (' Start wash ... ')         if __name__== ' __main__ ':     w=washer () # # #    W.set_water ( #这两行代码注释掉, on behalf of the user does not have to set water and washing dosage. #    W.set_scour (2)     W.start_wash () 29

But in this way, can only add 10L water and 2L detergent, not good, to improve. Use the constructor function.

1 class Washer:2  3     def __init__ (self,water=10,scour=2): 4         self.water=water 5         self.scour=scour 6  7     def set_water (self,water): 8         self.water=water         9     def set_scour (self,scour): one         self.scour= Scour     def add_water (self): +         print (' Add water: ', self.water)     def add_scour (self): 17         print (' Add scour: ', Self.scour)-     def start_wash (self):         self.add_water ()         self.add_ Scour ()         print (' Start wash ... ')         if __name__== ' __main__ ':     w=washer ()     W.start_wash ()     Wb=washer (100,10)     Wb.start_wash ()     wc=washer (100,10) +     Wc.set_water (     +) Wc.set_scour (5)     Wc.start_wash ()     39

Operation Result:

Constructors enable classes to enrich instance objects as they are instantiated, with the effect of instantiating different kinds of instances.

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.