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 > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the object-oriented detailed explanation of the basic knowledge of Python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "the object-oriented detailed explanation of the basic knowledge of Python".
Object-oriented fundamentals:
At the beginning of Python design is an object-oriented language, object-oriented is actually a programming idea, now people agree that the explanation is: encapsulation, inheritance, polymorphism is object-oriented.
Encapsulation, inheritance and polymorphism are indeed a kind of object-oriented embodiment. I feel that this explanation is far-fetched, the object-oriented idea is more abstract, and there is no better explanation for the time being.
What is object-oriented:
Object-oriented programming (Object Oriented Programming) referred to as OOP: (interprets unofficially) abstracts a class of concrete things from their characteristics, behavior, analysis, design and implementation of a complete program.
The realization process of object-oriented thought:
Object-oriented analysis (OOA, Object-Oriented Analysis)
Object-oriented design (OOD, Object-Oriented Design)
Object-oriented programming (OOP, Object-Oriented Program)
Object-oriented testing (OOT,Object-Oriented Test)
Object oriented maintenance (OOSM,Object-Oriendted Soft Maintenance)
Programmers think that everything is an object and can be coded. Anyone who has known the common sense of the program knows that programmers always say that there is no object. There is no object new one! This is actually a stem of instantiated objects in the Java language. Python is called the most beautiful and concise language, and the definition of instantiated objects and classes is really a little simpler than Java.
Today we're going to talk about Class in Python, not to mention too much, for example: now I'm going to count what phones, brands, and memories are used by people I know. Wait, wait, wait.
Xxx_phone_name = 'iphone11'xxx_phone_screen_size =' 6.1in 'xxx_phone_color =' black 'xxx_phone_ram =' 3GB'xxx_phone_rom = '128GB'xxx_phone_camera =' 12 million 'xxx_phone_battery =' 3200mAh'xxx_phone_price = 5499xxx_phone_use = 'playing video games' #...
This statistics is troublesome to death, statistics hundred and eighty are tired of hematemesis, so let's get to know the class.
Class:
A complex custom data type whose parameters can be any data type.
The definition format of the class:
Class (parent class list): class variables. Initialization method (self, object variable...) Class method static method attribute member method / object method destructor
The details of the above will be introduced later, first of all, let's use the object-oriented method to analyze it. At present, our demand is to count the brand, screen size, color, running memory size, storage size, pixels, battery size, price, and what this person mainly uses. So we stipulate that the object of the mobile phone must have these parameters, and the program written with the class method looks like this:
Class Phone: def _ _ init__ (self, name, screen_size, color, ram, rom, camera, battery, price Use): self.name = name self.screen_size = screen_size self.color = color self.ram = ram self.rom = rom self.camera = camera self.battery = battery self.price = price self.use = use def do (self): print ('frequent use of mobile phones' + self.use) xxx_phone = Phone ('iphone11',' 6.1in', 'black' Print (brand: {}, screen size: {}, color: {}, running memory: {}, body storage: {}, pixels: {}, battery capacity: {}, price: {} '.format (xxx_phone.name, xxx_phone.screen_size, xxx_phone.color, xxx_phone.ram) Xxx_phone.rom, xxx_phone.camera, xxx_phone.battery, xxx_phone.price)) xxx_phone.do ()
The running results are as follows:
Brand: iphone11, screen size: 6.1in, color: black, running memory: 3GB, body storage: 128GB, pixel: 12 million, battery capacity: 3200mAh, price: 5499 often use mobile phone to play games
Although there seems to be more code now than before, which is more convenient to think about tens of thousands of pieces of data?
Note:
1. The first letter of the class name is capitalized, in order to distinguish it from the file name (module name), function name, etc., this is a specification (non-specified)
2. Be sure to initialize the function
3. Self, the first parameter of the initialization function, can be changed, but it is not recommended. Python language uses self, which instantiates self to represent who, to distinguish between member variables and object variables of the same name.
The basic running process of the program:
The construction process of the object:
1. Open up space for the object, use new construction method 2, call initialization method init initialization 3, and return the reference of the object.
Class Phone: def _ _ new__ (cls,*args,**kwagrs): print ('I execute first, open up memory space') # returns and calls the parent class new constructor. Object is the parent class return object.__new__ (cls) def _ init__ (self,name): self.name = name print ('instantiated object') # destructor Def _ del__ (self): print ('execute me before the object is destroyed') phone1 = Phone ('iphone 11') phone2 = Phone (' iphone x') phone3 = Phone ('iphone 8') phone4 = Phone (' iphone 7') phone4 = Phone ('iphone 11') print (phone1.name) print (phone2.name) print (phone3.name) print (phone4.name)
The results are as follows:
I execute first, open up memory space to instantiate the object, execute me before the iphone 11iphone xiphone 8iphone 11 object is destroyed, execute me before the object is destroyed
The phone4 variable points to the Phone ('iphone 7') object first and then to the Phone (' iphone 11'), so the Phone ('iphone 7') object is destroyed first, which can be understood as the previous object covered by the later object, and all the objects destroyed before the end of the program.
Garbage collection mechanism: (to put it briefly)
The object in the program is released by the garbage collector (garbage collector). Before collection, the destructor of the corresponding class of the object will be automatically called to prepare for the release (closing the occupation of resources, etc.).
Recycling mechanism:
1. Reference count
2. Mark removal
3. Generation by generation recovery
The system automatically provides a default no-parameter initialization method for each class.
If you customize the initialization method, the no-parameter initialization method is no longer available. The Java language must provide a no-argument constructor, but Python does not, and the following two pieces of code are the same.
Class Test (): passt = Test () print (t) class Test1 (): def _ _ init__ (self): passt1 = Test1 () print (T1)
The results are as follows:
The memory address printed is the memory address of the object. If you print the instantiated object directly and want to print in your own format, you need to override the str method, such as:
Class Phone: def _ init__ (self, name): self.name = name def _ str__ (self): return 'phone is:' + self.namephone1 = Phone ('Apple') phone2 = Phone ('Huawei') print (phone1) print (phone2)
The results are as follows:
Mobile phones are: iPhone: Huawei
The relationship between classes and objects:
1. Class is a template for an object
two。 A class can create multiple objects,
3. An object is a specific individual of a class.
4. Objects have independent memory space and do not affect each other
= = with is:
= = generally used to compare values
Is is used to compare memory addresses and view memory addresses id (x)
Class Phone: def _ init__ (self, name): self.name = namephone1 = Phone ('iphone 11') phone2 = Phone (' iphone 11') print (phone1.name = = phone2.name) print (phone1 = = phone2) print (phone1 is phone2)
The results are as follows:
TrueFalseFalse
Although the two instantiated objects look the same, they are not the same object, just like two identical mobile phones. If you lose your own mobile phone in different people's mobile phones, you can't think that other people's phones are your own.
If there is a special need, such as two different mobile phone objects, as long as the same name is considered equal, you need to override the eq method.
Class Phone: def _ init__ (self, name): self.name = name def _ eq__ (self, other): return self.name = = other.namephone1 = Phone ('iphone 11') phone2 = Phone (' iphone 11') print (phone1 = = phone2)
The result is:
True
Class attributes:
Now going back to the original example, counting our own mobile phones, they all have a common feature, that is, the owners of these mobile phones are ourselves. If we add the same parameter to each instantiated phone, it will be troublesome, so we introduce the following class properties: properties that all instantiated objects have.
Class Phone: owner = 'me' def _ init__ (self, name): self.name = namephone1 = Phone (' Huawei') phone2 = Phone ('Honor') print (Phone.owner) print ('this' + phone1.name+' phone belongs to: 'phone1.owner) print (' this'+ phone2.name+' phone belongs to:'+ phone2.owner) the execution result is: me belongs to Huawei phone: me belongs to me phone
Class properties, the class can be called directly, and all the generated objects can also be called.
Summary
Object-oriented and process-oriented: two different programming ideas, process-oriented is a step-by-step, step-by-step implementation. Object-oriented is to first understand the requirements, design templates, mass production. It's like two people going to harvest wheat. The process-oriented approach goes like this:
Take sickle 2, harvest wheat 3, transport home 4, harvest wheat 5, transport home 6.
The process-oriented idea is that the demand is to harvest the wheat, build a harvester, and then harvest the wheat all the time. The two ideas have different scenarios, just as the wheat to be harvested is only the size of a slap, and thousands of acres of fertile land.
At this point, I believe you have a deeper understanding of the "object-oriented detailed interpretation of Python basic knowledge". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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.