In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article shares with you the content of sample analysis of Python classes and objects. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Object-oriented (1) programmers are "object-oriented"
There are all kinds of different forms of things in the real world, and there are all kinds of connections between these things. In the program, we use objects to map things in reality, and use the relationship between objects to describe the relationship between things. This idea is object-oriented.
(2) two kinds of programming ideas to realize Gobang
Process-oriented and object-oriented programming thought
If you add the remorse chess function, process-oriented and object-oriented, how to achieve it respectively?
(3) process oriented vs. object-oriented
Process-oriented is to analyze the steps needed to solve the problem, and then use functions to implement these steps one by one, and then call them in turn when you use them. (POP:Procedure-Oriented Programming)
Object-oriented is to divide the transaction that constitutes the problem into several independent objects according to certain rules, and then solve the problem by calling the method of the object. (OOP:Object-Oriented Programming)
(4) three major characteristics of object-oriented
Object-oriented is to divide the transaction that constitutes the problem into several independent objects according to certain rules, and then solve the problem by calling the method of the object. An application will contain multiple objects, and the function of the application can be realized through the cooperation of multiple objects, so that when the function of the application changes, only individual objects need to be modified, thus making the code easier to maintain. The characteristics of object-oriented can be summarized as encapsulation, inheritance and polymorphism.
1. Encapsulation (encapsulation)
Encapsulation is the core idea of object-oriented, which encapsulates the property and behavior of the object without letting the outside world know the specific implementation details.
2. Inheritance (inheritance)
Inheritance mainly describes the relationship between classes. Through inheritance, the function of the original class can be extended (extend) without rewriting the original class.
3. Polymorphism (polymorphism)
Polymorphism means that when the attributes and functions defined in one class are inherited by other classes, when the subclass object is directly assigned to the parent class reference variable, the variable of the same reference type calls the same method showing many different behavior characteristics.
II. Class and object (1) relationship between class and object 1. Overview
The idea of object-oriented programming tries to make the description of things in the program consistent with the shape of things in reality. In order to do this, two concepts-class and object-are put forward in the object-oriented idea.
A class is an abstract description of a class of things, while an object is used to represent the individual of such things in reality. In short, the class is the template and the object is the instance.
2. Schematic diagram
Analysis: the person in the picture above can be regarded as a class, and each specific person (such as Xiao Han, Xiao Shi, etc.) can be regarded as the object. The relationship between the class and the object can be seen from the relationship between the person and the specific individual.
Description: the class is used to describe the common characteristics of multiple objects, it is the template of the object, while the object is used to describe the individual in reality, it is an instance of the class. An object is the embodiment of a class, and a class can correspond to multiple objects.
(2) definition of class 1. Composition of class
Class name: class name, the first letter must be capitalized, such as Person
Class properties: a set of data, such as name, sex, age... (describe the characteristics of the class-static)
Class methods: methods that allow operations, such as talking, walking, running, etc. (describe the behavior of the class-dynamic)
2. Class definition syntax format
Use the class keyword to declare a class
Class class name: the method of the property class of the class
The defined class inherits one or more parent classes
Class class name (parent class 1, parent class 2, … , parent class n): method of the attribute class of the class 3. Case demonstration
Write a program-class definition demo .py
(3) create object 1. Syntax format
Create an object using the no-parameter construction method: object name = class name ()
Create an object using a parametric construction method: object name = class name (parameter list)
2. Add attributes to the object
Object name. New attribute name = value
3. Case demonstration
Write a program-create an object based on a class. py
Run the program to view the results
Modify the object property value, and then access the object method
Thus, three properties name, gender and age are defined in the Person class, and all objects based on this class have the same initial value, and then you can modify the property value of the object as needed.
Create another object father based on the Person class, call the speak () method, modify the object property value, and then call the speak () method
III. Overview of objects in Python (1)
Previously, we have used a variety of objects: integer objects, decimal objects, string objects, function objects, module objects, and so on.
Use the dir () function to see what properties and methods an object has
(2) commonly used objects 1 and integer objects
(1) View the properties and methods of integer objects
Execute: dir (int)
['_ _ abs__','_ _ add__','_ _ and__','_ _ bool__','_ _ ceil__','_ _ class__','_ _ delattr__','_ _ dir__','_ _ divmod__','_ _ doc__','_ eq__','_ float__','_ _ floor__','_ _ floordiv__' '_ _ format__',' _ _ ge__','_ _ getattribute__','_ _ getnewargs__','_ _ gt__','_ _ hash__','_ _ index__','_ _ init__','_ _ init_subclass__','_ _ int__','_ invert__','_ le__','_ lshift__','_ _ lt__' '_ _ mod__',' _ _ mul__','_ _ ne__','_ _ neg__','_ _ new__','_ _ or__','_ _ pos__','_ _ pow__','_ _ radd__','_ _ rand__','_ rdivmod__','_ reduce__','_ reduce_ex__','_ _ repr__' '_ _ rfloordiv__',' _ _ rlshift__','_ _ rmod__','_ _ rmul__','_ _ ror__','_ _ round__','_ _ rpow__','_ _ rrshift__','_ _ rshift__','_ _ rsub__','_ rtruediv__','_ rxor__','_ setattr__','_ _ sizeof__' '_ str__',' _ _ sub__','_ _ subclasshook__','_ _ truediv__','_ _ trunc__','_ _ xor__', 'as_integer_ratio',' bit_length', 'conjugate',' denominator', 'from_bytes',' imag', 'numerator',' real', 'to_bytes']
(2) case demonstration
View the byte length of the integer-bit_length ()
The binary number of 327 is 9. Call the bin () function to convert it to binary: 101000111 (9 bits)
View the real and imaginary parts of the plural
Convert integers to byte data-to_bytes ()
Demonstrate converting integer 1345 to byte data
Demonstrate converting byte data to integers
Demonstrate exponentiation-_ _ pow__ ()
Demonstrate comparison operation
2. Decimal object
(1) View the properties and methods of decimal objects
Execute: dir (float)
['_ _ abs__','_ _ add__','_ _ bool__','_ _ class__','_ _ delattr__','_ _ dir__','_ _ divmod__','_ _ doc__','_ _ eq__','_ _ float__','_ floordiv__','_ format__','_ _ ge__','_ _ getattribute__' '_ _ getformat__',' _ _ getnewargs__','_ _ gt__','_ _ hash__','_ _ init__','_ _ init_subclass__','_ _ int__','_ _ le__','_ _ lt__','_ _ mod__','_ mul__','_ ne__','_ neg__','_ _ new__' '_ _ pos__',' _ _ pow__','_ _ radd__','_ _ rdivmod__','_ _ reduce__','_ _ reduce_ex__','_ _ repr__','_ _ rfloordiv__','_ _ rmod__','_ _ rmul__','_ round__','_ rpow__','_ rsub__','_ _ rtruediv__' '_ _ set_format__',' _ _ setattr__','_ _ sizeof__','_ _ str__','_ _ sub__','_ subclasshook__','_ _ truediv__','_ _ trunc__', 'as_integer_ratio',' conjugate', 'fromhex',' hex', 'imag',' is_integer', 'real']
(2) case demonstration
Keep decimal places-_ _ round__ ()
Demonstrate exponentiation-_ _ pow__ ()
3. String object
(1) View the properties and methods of the string object
Execute: dir (str)
['_ _ add__','_ _ class__','_ _ contains__','_ _ delattr__','_ _ dir__','_ _ doc__','_ _ eq__','_ _ format__','_ _ ge__','_ _ getattribute__','_ getitem__','_ getnewargs__','_ _ gt__','_ _ hash__' '_ _ init__',' _ _ init_subclass__','_ _ iter__','_ _ le__','_ _ len__','_ _ lt__','_ _ mod__','_ _ mul__','_ _ ne__','_ _ new__','_ reduce__','_ reduce_ex__','_ repr__','_ _ rmod__' '_ rmul__',' _ setattr__','_ sizeof__','_ str__','_ subclasshook__', 'capitalize',' casefold', 'center',' count', 'encode',' endswith', 'expandtabs',' find', 'format',' format_map', 'index',' isalnum', 'isalpha',' isascii', 'isdecimal',' isdigit', 'isidentifier' 'islower', 'isnumeric',' isprintable', 'isspace',' istitle', 'isupper',' join', 'ljust',' lower', 'lstrip',' maketrans', 'partition',' replace', 'rfind',' rindex', 'rjust',' rpartition', 'rsplit',' rstrip', 'split',' splitlines', 'startswith',' strip', 'swapcase',' title', 'translate' 'upper',' zfill']
(2) case demonstration
Determine whether the string is made up of all numbers-isdigit ()
Determine whether the string is made up of all letters-isalpha ()
4. Function object
Define function
View function types
Assign a function object to a variable
5. Module object
Import modules and view types
Access the properties and functions of the module
Assign a module object to a variable
(3) two kinds of objects
In the object model of Python, there are two kinds of objects: class objects and instance objects. The class object is created when the class statement is executed, while the instance object is created when the class is called. Each time the class is called, an instance object is created. There is only one class object, while there can be multiple instance objects. Class objects and each instance object have their own namespaces and store their own data in their own namespaces.
1. Class object
When Python executes the class statement, it creates a class object and a variable (the name is the class name), and the variable references the class object. Like def, class is an executable statement. When the class module is imported, the class statement is executed to create the class object.
The variables created by the assignment statements in the class are the data properties of the class. Similar to modules, the variables created by the top-level assignment statements in the class belong to the class object. The data data of the class is accessed in the format of "object name. property name.
The function defined by the def statement in the class is the method property of the class and is accessed in the format "object name. method name ()".
Class data and methods are shared by all instance objects.
2. Instance object
The instance object is created by calling the class object (just as a function is called).
Each instance object inherits the properties of the class object and gets its own namespace.
The private property of the instance object. The first argument to the method function of the class defaults to self, which represents the object instance that references the method. Assigning a value to the property of self in the method creates the property that belongs to the instance object.
Thank you for reading! This is the end of this article on "sample Analysis of Python classes and objects". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.