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 "Python object-oriented example analysis". In daily operation, I believe many people have doubts about Python object-oriented example analysis. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "Python object-oriented example analysis". Next, please follow the editor to study!
1 introduction
Object-oriented (OOP) is a method of understanding and abstracting the real world, the meaning of object refers to the concrete things that can be seen and touched in real life, a more classic description is that everything is an object, Python is an object-oriented language, object-oriented programming is simply a way of encapsulating code.
Object-oriented related concepts
Class: describes a collection with the same properties and methods, simply a template that can be used to create objects.
Object: an instance of a class.
Method: the function defined in the class.
Class variable: a variable defined in a class and outside a function, common to all instantiated objects.
Local variable: a variable defined in a method that acts only on the current instance.
Three characteristics of object-oriented
Encapsulation: hides the properties and implementation details of the object, only provides public access to the outside world, and improves reusability and security.
Inheritance: a class that inherits a base class can have the properties and methods of the base class, which can improve the reusability of the code.
Polymorphism: the reference variable defined by the parent class can point to the instance object of the subclass, which improves the extensibility of the program.
2 basic operation
2.1 category
The definition of a class in Python uses the class keyword, and the syntax is as follows:
Class class name:
Attribute
...
Method
...
For example, we define a class Cat, as follows:
Class Cat:
# attribute
Color = 'black'
# Construction method
Def _ _ init__ (self, name):
Self.name = name
# Custom method
Def eat (self, food):
Self.food = food
Print (self.name, 'eating' + food)
The constructor _ _ init__ () is called automatically when the class is instantiated. Both constructors and other methods need to take self as the first parameter, which represents an instance of the class.
After the class is created, we can access the property directly through the class name in the format: class name. The property name, such as we access the color property of the Cat class, is as follows:
Print ('color-- >', Cat.color)
The properties and methods defined in the Cat class above are public. In addition, we can also define private properties and methods by adding two underscores to the property name or method name. The example is as follows:
Class Cat:
_ _ cid ='1'
Def _ run (self):
Pass
It is important to emphasize that if private properties and methods cannot be accessed externally, the Cat.__cid will naturally report an error.
2.2 object
Creating an object is also called class instantiation. For example, we create an object through the Cat class, as shown below:
# create an object
C = Cat ('Tom')
Once the object is created, we can use it to access properties and invoke methods, as shown below:
# access attributes
Print ('name-- >', c.name)
Print ('color-- >', c.color)
# calling method
C.eat ('fish')
Similarly, object c cannot access the private property _ _ cid and call the private method _ _ run, so we have a question: is this private property and method defined but cannot be used?
Let's take a look at an example, as follows:
Class Cat:
_ _ cid ='1'
Def _ _ run (self, speed):
Print ('_ _ cid is the cat of'+ self.__cid+'', 'running at the speed of' + speed+'')
Def run (self, speed):
Self.__run (speed)
C.run ('50 miles')
Output result:
_ _ cid is a cat that runs at a speed of 50 mph
From the example, we can see that internally private properties and methods can be accessed and called.
2.3 inheritance
Python supports class inheritance and multiple inheritance. The syntax format is as follows:
Class base class (subclass 1, subclass 2...):
... Http://www.sptdfk.com/ of Zhengzhou Gynecology Hospital
The example is as follows:
# Persian cats
Class PersianCat (Cat):
Def _ _ init__ (self, name):
Self.name = name
Def eat (self, food):
Print (self.name, 'eating' + food)
# Garfield cats
Class GarfieldCat (Cat):
Def _ _ init__ (self, name):
Self.name = name
Def run (self, speed):
Print (self.name, 'running at a speed of' + speed+'')
# single inheritance
Class SingleCat (PersianCat):
Pass
# multiple inheritance
Class MultiCat (PersianCat, GarfieldCat):
Pass
# call
Sc = SingleCat ('Persian Cat No.1')
Sc.eat ('fish')
Mc = MultiCat ('Persian Garfield No.1')
Mc.eat ('fish')
Mc.run ('50 miles')
If the inherited parent class method does not meet our needs, the subclass can override the parent class method, as follows:
Class SingleCat (PersianCat):
Def eat (self, food):
Print (self.name, 'eating' + food, 'after ten minutes', self.name+' is full')
Sc = SingleCat ('Persian Cat No.1')
Sc.eat ('fish')
At this point, the study of "Python object-oriented example Analysis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.