In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you the "Python object-oriented and class example analysis", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Python object-oriented and class example analysis" this article.
One and two major programming ideas
II. Classes and objects
A simple example:
Everything in python is an object. Here is an opening picture:
Define classes in Python
Example 1:
Class Student: passprint (id (Student)) # 1149225945800print (type (Student)) # print (Student) #
Example 2:
Class Student: native_place=' Jilin'# Class attribute def _ _ init__ (self,name Age): self.name=name self.age=age # instance method def eat (self): print ("students are eating") # static method @ staticmethod def method (): print ("I am a static method") # class method @ classmethod def cm (cls): print ("I am a class method") IV. Object creation
Example 1:
# instance object student1=Student ("Zhang San", 18) print (student1) print (id (student1)) print (type (student1)) print ("- -") # object, representing the class print (Student) print (id (Student)) print (type (Student))
Example 2:
# instance object student1=Student ("Zhang San", 18) print (student1.name) print (student1.age) # instance method calls have the following two uses: print (student1.eat ()) print (Student.eat (student1)) 5. Class properties, class methods, static methods
Example 1: class attributes
# Class attribute student1=Student ("Zhang San", 18) student2=Student ("Li Si" 19) print (Student.native_place) # Jilin print (student1.native_place) # Jilin print (student2.native_place) # Jilin Student.native_place=' Sichuan 'print (student1.native_place) # Sichuan print (student2.native_place) # Sichuan #-- -student1.native_place=' Guangdong 'print (student1.native_place) # Guangdong print (student2.native_place) # Sichuan
Example 2: class method, static method
# Class methods, static methods use student1=Student ("Zhang San", 18) Student.method () # I am static method Student.cm () # I am class method VI, dynamic binding properties and methods
Python is a dynamic language. After creating an object, you can dynamically bind properties and methods.
For example: attribute binding
Class Student: def _ init__ (self,name,age): self.name=name self.age=age # instance method def eat (self): print ("students are eating") student1=Student ('Zhang San', 19) student2=Student ('Li Si' 20) print (id (student1)) # 2363920157896print (id (student2)) # 2363920157960print ("- bind attribute -") print ("bind attribute-dynamic binding gender property for student2 -") student2.gender=' male 'print (student1.name,student1.age) # Zhang San 19#print (student1.gender) when student1 accesses attributes that it does not have Error AttributeError: 'Student' object has no attribute' gender'print (student2.name,student2.age,student2.gender) # Li Si 20 male print ("- bind method -") def show (): print ('I am the show method') student1.show=showstudent1.show () # I am the show method student2.show () # error AttributeError: 'Student' object has no attribute' show'
Memory analysis:
VII. Three major features of object-oriented
1. Encapsulation
Class Car: def _ _ init__ (self,brand,age): self.brand=brand self.__age=age def show (self): print (self.brand,self.__age) car1=Car ('BMW X5 minute print 50) print (car1.brand) # the attribute of the BMW Xbike print (car1.__age) _ _ logo restricts its use outside the class, and can be used inside the class. External access will report an error # to use the attribute identified by _ _, you can first use dir () to find the attribute Then access print (dir (car1)) # output ['_ Car__age','_ _ class__','_ _ delattr__','_ _ dict__','_ _ dir__','_ _ doc__','_ _ eq__','_ format__','_ ge__','_ getattribute__','_ gt__','_ _ hash__','_ init__' '_ _ init_subclass__',' _ _ le__','_ _ lt__','_ _ module__','_ _ ne__','_ _ new__','_ _ reduce__','_ _ reduce_ex__','_ _ repr__','_ _ setattr__','_ sizeof__','_ str__','_ subclasshook__','_ _ weakref__' 'brand',' show'] print (car1._Car__age) # 50
2. Inheritance (unlike other languages, python supports multiple inheritance)
For example:
Class People: def _ init__ (self,name,age): self.name=name self.age=age def info (self): print (self.name,self.age) class Student (People): def _ init__ (self,name,age,sno): super (). _ init__ (name,age) self.sno=snoclass Teacher (People): def _ init__ (self,name,age) Teachofage): super (). _ init__ (name,age) self.teachofage=teachofagestudent1=Student ('Zhang San', 18122) teacher1=Teacher ('Li Si', 36 Magi 10) student1.info () # Zhang San 18teacher1.info () # Li Si 368, method rewrite
For example:
Class People: def _ init__ (self,name,age): self.name=name self.age=age def info (self): print (self.name,self.age) class Student (People): def _ init__ (self,name,age,sno): super (). _ init__ (name) Age) self.sno=sno def info (self): super (). Info () print (self.sno) class Teacher (People): def _ init__ (self,name,age,teachofage): super (). _ init__ (name,age) self.teachofage=teachofage def info (self): super (). Info () print (self.teachofage) student1=Student ('Zhang San', 18122) teacher1=Teacher ('Li Si') 36 student1.info 10) teacher1.info ()
The result is:
The above is all the content of the article "Python object-oriented and Class sample Analysis". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.