In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the definition and usage of classes in Python". In daily operation, I believe that many people have doubts about the definition and usage of classes in Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "definition and usage of classes in Python". Next, please follow the editor to study!
Catalogue
1. Definition of class
2. Create an object
3. Inheritance
1. Definition of class
Create a rectangle.py file and define a Rectangle class in that file. In this class, _ _ init__ represents the constructor. Where the self parameter is the first parameter in each class definition method (here it can also be other variable names, but Python often uses the variable name of self). When you create an object, the self parameter in each method points to and references the object, which is equivalent to a pointer. In this class, the constructor indicates that the class has two properties, _ width and _ height (also called instance variables), and assigns an initial value of 1 to them.
The _ _ str__ method represents the object as a string, which is easy to print and display, which is equivalent to overriding the toString method in Java. Where _ _ init__ and _ _ str__ are the basic methods provided by the class.
Class Rectangle: # Construction method def _ init__ (self, width=1, height=1): self._width = width self._height = height # State representation method def _ str__ (self): return ("Width:" + str (self._width) + "\ nHeight:" + str (self._height)) # assignment method def setWidth (self Width): self._width = width def setHeight (self, height): self._height = height # value method def getWidth (self): return self._width def getHeight (self): return self._height # other methods def area (self): return self._width * self._height2, create object
Create a new Test.py file and call the class of Rectangle in the rectangle module.
Import rectangle as recr = rec.Rectangle (4,5) print (r) print () r = rec.Rectangle () print (r) print () r = rec.Rectangle (3) print (r)
Then output the result:
The _ _ str__ method is called directly by the object of the print Rectangle class. The figure above shows three different ways to construct parameters in a method when initializing a Rectangle object.
There are two forms of creating an object, and its pseudo code is represented as:
1) objectName = ClassName (arg1,arg2, … )
2) objectName = moduleName.ClassName (arg1,arg2, … )
The variable represented by the variable name objectName points to the object type.
3. Inheritance
If you add attributes to the parent class, the subclass must first contain initialization methods that depict the properties of the parent class, and then add new properties of the subclass. The pseudo code is as follows:
Super (). _ _ init _ _ (parentParameter1,... , parentParameterN)
Create a new square.py file:
Import rectangle as recclass Square (rec.Rectangle): def _ _ init__ (self, square, width=1, height=1): super (). _ init__ (width Height) self._square = square def _ str__ (self): return ("square side length is:" + str (self._width) + "\ narea is:" + str (self._square)) def isSquare (self): if self._square = = self.getWidth () * self.getWidth (): return True else: Return Falses = Square (1) print (s) print (s.isSquare ()) s = Square (2) print (s) print (s.isSquare ())
Output:
At this point, the study of "the definition and usage of classes in Python" 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.