Learning: Python learning path (6): class and Object
In layman's terms, classes are definitions, and objects are entities.
Simply put, people are classes, and Gao Peng is an object.
Attribute
Attributes include instance attributes and class attributes.
Let's take a look at the previous Code:
- Class Fruit:
- Price = 0
-
- Def _ init _ (self ):
- Self. color = 'red'
- Zone = "china"
-
- If _ name __= = "_ main __":
- Print "Fruit price: % d" % Fruit. price
- Apple = Fruit ()
- Print "apple color: % s" % apple. color
- Print "apple price: % d" % apple. price
- Banane = Fruit ()
- Print "banane color: % s" % banane. color
- Print "banane price: % d" % banane. price
-
- Fruit. color = "yellow"
- Fruit. price = 50
-
- Print "apple color: % s" % apple. color
- Print "apple price: % d" % apple. price
- Print "banane color: % s" % banane. color
- Print "banane price: % d" % banane. price
- # Result
- Fruit price: 0
- Apple color: red
- Apple price: 0
- Banane color: red
- Banane price: 0
- Apple color: red
- Apple price: 50
- Banane color: red
- Banane price: 50
You can directly modify the class attributes of all classes through the class, but cannot modify the instance attributes.
Modify rows 17 and 18.
- Class Fruit:
- Price = 0
-
- Def _ init _ (self ):
- Self. color = 'red'
- Zone = "china"
-
- If _ name __= = "_ main __":
- Print "Fruit price: % d" % Fruit. price
- Apple = Fruit ()
- Print "apple color: % s" % apple. color
- Print "apple price: % d" % apple. price
- Banane = Fruit ()
- Print "banane color: % s" % banane. color
- Print "banane price: % d" % banane. price
-
- Apple. color = "yellow"
- Apple. price = 50
-
- Print "apple color: % s" % apple. color
- Print "apple price: % d" % apple. price
- Print "banane color: % s" % banane. color
- Print "banane price: % d" % banane. price
- # Result
- Fruit price: 0
- Apple color: red
- Apple price: 0
- Banane color: red
- Banane price: 0
- Apple color: yellow
- Apple price: 50
- Banane color: red
- Banane price: 0
Modifying instance variables only affects the instance variables and the class attributes of the instance.
Let's try again.
- Class Fruit:
- Price = 0
-
- Def _ init _ (self ):
- Self. color = 'red'
- Zone = "china"
-
- If _ name __= = "_ main __":
- Print "Fruit price: % d" % Fruit. price
- Apple = Fruit ()
- Print "apple price: % d" % apple. price
- Banane = Fruit ()
- Print "banane price: % d" % banane. price
-
- Apple. price = 30
- Fruit. price = 50
-
- Print "apple price: % d" % apple. price
- Print "banane price: % d" % banane. price
- # Result
- Fruit price: 0
- Apple price: 0
- Banane price: 0
- Apple price: 30
- Banane price: 50
If the class attribute of the instance is modified, it will be detached from the class attribute.
Class Method
- Class Fruit:
- Price = 0
-
- Def _ init _ (self ):
- Self. color = 'red'
- Zone = "china"
- Def printColor (self ):
- Print "color:" + self. color
- Def printPrice (self ):
- Print "price: % d" % self. price
- @ Staticmethod
- Def printStatic ():
- Print "static"
-
-
-
- If _ name __= = "_ main __":
- Fruit. printStatic ()
- Apple = Fruit ()
- Apple. printStatic ()
- Apple. printPrice ()
- Apple. printColor ()
The common method comes with the self parameter, which allows you to access instance attributes and class attributes through self. Static methods do not contain self parameters, nor can they access instance parameters and class parameters through self.
- Class Fruit:
- Price = 0
- @ Staticmethod
- Def printStatic ():
- Print Fruit. price
-
- If _ name __= = "_ main __":
- Fruit. printStatic ()
It can only be accessed directly through a class.
Constructor and destructor
- Class Fruit:
- Count = 0
- Def _ init _ (self ):
- Print "I have been called"
-
- Def _ del _ (self ):
- Print "I was unfortunately called"
-
- If _ name __= = "_ main __":
- Apple = Fruit ()
- # Result
- I have been called
- Unfortunately, I was called.
-
-
- Class Fruit:
- Count = 0
- Def _ init _ (self ):
- Print "I have been called"
-
- Def _ del _ (self ):
- Print "I was unfortunately called"
-
- If _ name __= = "_ main __":
- Apple = Fruit ()
- Fruit. count
- # Result
- I have been called
- Unfortunately, I was called.
Class initialization and destruction are called.
The class is not automatically called when it is called.
Summary
This time I made some basic introductions to the class. Next time we will talk about inheritance and polymorphism.