In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the Python object-oriented class and object example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
What is object-oriented programming?
Have we ever heard of process-oriented, which is easier to understand when compared with each other?
To put it simply, process-oriented means to solve all files with functions, simple and rude!
Object-oriented appeared after process-oriented programming, and many programs have not been developed without object-oriented programming.
Object-oriented, also uses functions, but with a net that associates one or more functions with data, and then calls it a class of things, that is, the class in the program.
Define the class and feel it from the specific code!
Object-oriented programming, the first concept put forward is the 'class', class:
# this is a class definition code: class hello_class (): pass
It is then called to produce the object through class_name ().
With a slight code update, let's take a look at:
Class hello_class (): pass# output class information print (hello_class) print (type (hello_class)) # create an instance object of the class print (hello_class ()) print (type (hello_class ()
To add a little bit:
The print function outputs the result of a class object: usually
The following is the running result:
Here we add new knowledge points: class instance objects, usually directly, instances.
An instance is an object generated by class, and the type of all hello_class objects (obtained through the type function) must be hello_class.
Observation of multiple classes and objects
After looking at a class, let's take a look at the comparison of the two classes, and the results are consistent.
The following is the definition of the two classes and the code demonstration of the generated object:
#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 11:58 on 2021-11-15 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Ray academic Committee # @ XueWeiTag: CodingDemo# @ File: _ _ init__.py.py# @ Project: helloclass student (object): "" the academic Committee adds that the _ _ init___ function is the initialization function of the class. This function is called when an instance of the class object is created. Def _ init__ (self): print ("hello, I am student") class programmer (object): def _ init__ (self): print ("hello, I am programmer") class student (object): def _ init__ (self): print ("hello, I am student") class programmer (object): def _ init__ (self): print ("hello" I am programmer ") S1 = studentprint (S1) p1 = programmerprint (p1) S11 = studentprint (S11) p11 = programmerprint (p11) print (" * "* 16) # create object S2 = student () print (S2) p2 = programmer () print (p2) # create object S3 = student () print (S3) p3 = programmer () print (p3)
Explain a little bit:
The printout of the variables S1 and p1 is of type 'class''.
The printout of the two variables S11 and p11 is of the 'class' type, but S1 and S11 are the same as p1 and p11.
The printout of the variables S2 and p2 is of type 'object''.
The printout of the variables S3 and p3 is of type 'object''.
The following is the running result:
The initialization function is called to print the object information.
At this point, everyone should know the difference between class and object.
Class: describes the fixed relationship between functions and attributes
Object: a living individual based on this fixed relationship whose id is variable.
Supplement the attributes of the class (data section)
The school committee defined a student class and created two student objects.
Copy directly and run the following code:
#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 11:58 on 2021-11-15 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Raytheon Committee # @ XueWeiTag: CodingDemo# @ File: _ _ init__.py.py# @ Project: helloclass student (object): def _ init__ (self) Name): self.name = name def get_name (self): return self.name def set_name (self, name): self.name = name def study (self): print (f "{self.name}: study hard Up every day! ") S1 = student ("Xiaobai") print (S1) print (s1.get_name ()) print (s1.study ()) S2 = student ("student fans of the committee: ha") print (S2) print (s2.get_name ()) print (s2.study ())
We see that their id is always different (run it a few times).
Then each student has a name attribute (with name data) and three function attributes (used to get names, rename, and learn, respectively).
Then we call each student's study function and output their own learning status.
Thank you for reading this article carefully. I hope the article "sample Analysis of classes and objects in Python object-oriented" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.