For technicians who are not programmers , Understand a lot of things in the field of development thinking , It's actually a very painful thing , Until it comes into contact Python Class in , I learned what object-oriented is , More understanding is still through the actual code practice to be able to slowly grasp .
Today, let's record what object-oriented is , stay Python Why do we use classes in our programming world .
One 、 object-oriented
The process of abstracting the common features is object-oriented , This is my most intuitive understanding of object orientation , For example, we make cars in factories , Every car has its own production platform , Imagine a scene like this ,A The car has 100 Individual workers in independent production , among 20 Personal production chassis ,20 Personal R & D engine ,40 Personal frame 、 Car door ,20 Personal responsibility for final vehicle assembly ,B The car also has 100 Personal and A The same thing with a car , If the world is like this , Toyota 、 VW has long since gone out of business , Whatever it is , Although each car has its own independent production platform , But many things must have the same properties , For example, engine 、 The steering wheel 、 Car seats 、 tire , In the small screw cap and so on , We can completely separate these common things , Become the common property of each model , that 100 Workers may have 50 Individuals are in charge of the public part , Save labor cost at the same time , It also saves the cost of resources , For example, we need to adjust the size of the seat , So just for this 50 Personal work content can be adjusted , Instead of having to operate each car independently , This is the benefit that object-oriented brings to us .
Two 、Python Object orientation in —— class
A simple example of an object-oriented production scenario , We'll pass it again Python In function programming and class programming to compare their characteristics .
For example, there is a simple need , Is to develop a mail client , Through the program , Custom send what we want to send .
1、 Functional programming
Pseudo code
def send_mail(to_user, title, content): smtp_host = "mail.test1.com" smtp_username = "test_user1" smtp_password = "test_password1" smtp_obj = smtplib.SMTP(smtp_host, smtp_username, smtp_password) smtp_obj.sendmail(to_user, title, content) send_mail("[email protected]", "test_mail", "test_content")
Through the above pseudo code, we have simply realized the requirement of email sending , But if you want more people to use it , There will be some problems , For example, everyone's smtp、 The household registration order is not the same , We're going to change the code to look like this .
Pseudo code
def send_mail(smtp_host, smtp_username, smtp_password, to_user, title, content): smtp_obj = smtplib.SMTP(smtp_host, smtp_username, smtp_password) smtp_obj.sendmail(to_user, title, content) send_mail("mail.test1.com", "test_user1", "test_password1", "[email protected]", "test_mail", "test_content") send_mail("mail.test2.com", "test_user2", "test_password2", "[email protected]", "test_mail", "test_content") send_mail("mail.test3.com", "test_user3", "test_password3", "[email protected]", "test_mail", "test_content")
We also realize the function of multi person sending through the above-mentioned way , The program seems to be ok here , But if at this time our program requirements increase , For example, it's not just email , At the same time, there should be reception 、 Delete 、 And so on. , Our code looks like this again .
Pseudo code
def send_mail(smtp_host, smtp_username, smtp_password, to_user, title, content): smtp_obj = smtplib.SMTP(smtp_host, smtp_username, smtp_password) smtp_obj.sendmail(to_user, title, content) def recv_mail(smtp_host, smtp_username, smtp_password) smtp_obj = smtplib.SMTP(smtp_host, smtp_username, smtp_password)
mail Receive code
def delete_mail(smtp_host, smtp_username, smtp_password) smtp_obj = smtplib.SMTP(smtp_host, smtp_username, smtp_password)
mail Delete function
send_mail("mail.test1.com", "test_user1", "test_password1", "[email protected]", "test_mail", "test_content") recv_mail("mail.test1.com", "test_user1", "test_password1", recv_argv, ...) delete_mail("mail.test1.com", "test_user1", "test_password1", delete_argv, ...)
Let's see that only one user wants to achieve three functions , There's just so much code to write , At this point, the code is a little cumbersome , What's more deadly is if smtp The address of has changed , I'm going to change the parameters of each function , This obviously increases the cost of work and the chance of mistakes , But we found that each function has several common properties , Namely smtp Address ,smtp user ,smtp password , We can make these three attributes independent , Implemented through the encapsulation feature of the class , The following object-oriented programming .
2、 Object oriented programming
Python By creating a class , Can be a good object-oriented programming ideas , It's also the mail sender above , Let's implement it as a class .
Pseudo code
class MailTools: def __init__(self, smtp_host, smtp_username, smtp_password): self.smtp_host = smtp_host self.smtp_username = smtp_username self.smtp_password = smtp_password self.smtp_obj = smtplib.SMTP(smtp_host, smtp_username, smtp_password)
Send E-mail
def send_mail(self, to_user, title, content): self.smtp_obj.sendmail(to_user, title, content)
Receiving mail
def recv_mail(self, recv_argv, ...)
mail Receive code
Delete mail
def delete_mail(self, delete_argv, ...)
mail Delete the code
test_mail = MailTools("mail.test1.com", "test_user1", "test_password1") test_mail.send_mail(...) test_mail.recv_mail(...) test_mail.delete_mail(...)
We use the above class code , You create an object test_mail, Then call it below. send_mail、recv_mail Such method , This way we can make our code logic clearer , If smtp When parameters change , We only need to modify the parameters when the object is instantiated .
We use functional programming to implement simple functions , Because it's simple enough 、 Fast , But with the continuous upgrading of program functions 、 When multiple complex logical operations are provided , It's not so easy to use the function , So for different code scenarios , We need to use it well Python Programming mode in .
Python Knowledge points of middle class :
1、"__init__" Method is Python Construction method in , Used to initialize our class , That is to initialize the shared properties we want , At the same time, other functions in this class can be executed in the construction method .
2、self Is a formal parameter , Is the instantiated object of this class , for example a = My_Class() in , You can understand a Namely self Parameters .
3、 When you create an object , Parenthesis required after class , That is, to complete the instantiation of the class , meanwhile Python Will automatically find the constructor in this class .
4、 Class if there are more than one function defined , Then these functions can be called the methods owned by the instantiated object of this class .
5、 Three features of object-oriented programming : encapsulation 、 Inherit 、 polymorphic