Python foundation 6 -- class 1 (static class and dynamic class)

Posted by tommyboy123x on Tue, 08 Feb 2022 16:15:03 +0100

catalogue

1, Static class

2, Dynamic class

Classes are divided into static classes and dynamic classes. Static classes can be understood as a collection of functions with the same characteristics into one class. Dynamic classes are simply understood as instantiated classes. The key difference is that static classes do not have self keyword and do not support__ int__ Initialization function, that is, it cannot be instantiated.

1, Static class

In order to prevent code confusion, we can put the functions under the class. For example, we can put the functions with arms, legs, hands and feet under the human class. For example, if we want to set off firecrackers, eat dumplings and pay New Year's greetings in the new year, we can put these functions under the new year class.

Take an example:

class New_Year():            #Define a new class_ Year()
""""Create a new year's class" ""         #Documentation
    def go_home(self):       #Define a method go_ Home
        print("Don't isolate. You can go home!!!")  #The output is "no isolation, you can go home!!!"
    def eat_dumpling(self):  #Define a method eat_ Dumping (eating dumplings)
        print("It's delicious. I'm full!!!")  #The output is "delicious, full!!!"
    def fangpao(self):       #Define a method (a function can be called a method in a class) fangpao (shooting)
        print("Crackling")     #The output is "crackle"
    def bainian(self):       #Define a method Bainian
        print("Astro Boy Liu wishes you a happy New Year!!!")   #The output is "Astro Boy Liu wishes you a happy New Year!!!"

Tiger_Year = New_Year()     #New for class_ Create an instance of Tiger_Year
Tiger_Year.go_home()        #Call go via instance_ The home () method prints the result
Tiger_Year.eat_dumpling()   #Call eat through an instance_ The dumping () method prints the result
Tiger_Year.fangpao()        #Call the fangpao() method through the instance to print the result
Tiger_Year.bainian()        #Call the bainian() method through the instance to print the result

The print result is: no isolation, you can go home!!!
It's delicious. I'm full!!!
Crackling
Astro Boy Liu wishes you a happy New Year!!!

The above is a basic class, which defines a class for the new year. In the new year class, there are more than one hundred methods, such as going home, eating dumplings, shooting and centuries. The composition of the class is composed of the following contents

(1)class keyword

All class definitions must start with class, just as functions must start with def

(2) Class name

Naming of user-defined class name reference function

(3) First line format of class

class name ():

(4) Class document description

Use three single quotation marks ("" ") to refer to the description in pairs

(5) Class function

Class functions are also called methods in classes or instances. Go in the example_ home(),eat_ Dumping (), fangpao(), bainian() can also be called methods in a class

2, Dynamic class

Compared with static classes, dynamic classes can create instances. Instances can be understood as redefining a class and defining a value. This is instantiation, and this variable is an instance. For a class, it can be assigned to multiple instances, and the values in each instance do not interfere with each other., Examples are mainly composed of attributes and methods. In the above example__ int__ The content in the method can be called as an attribute, and the following other parameters can be called as methods.

Take a look at this example:

class Game():     #Define a Game class      
    def __init__(self,name,sex,age,fight):   #Define a reservation function__ int__()name,sex,age,fight
        self.name = name        #Define attribute variable name
        self.sex = sex          #Define attribute variable sex
        self.age = age          #Define attribute variable age
        self.fight = fight      #Define attribute variable fight

    def land(self):            #Define a method 
        """Land training combat effectiveness+200"""
        self.fight = self.fight - 200   #Combat effectiveness reduced by 200

    def sky(self):             #Define a method sky
        """Sky training combat effectiveness+100"""
        self.fight = self.fight + 100   #Combat effectiveness plus 100

    def sea(self):            #Define a method sea
        """Sea training combat effectiveness+300"""    
        self.fight = self.fight +300 #Combat effectiveness plus 300

    def xinxi(self):        #Define a method information
        temp = "full name:%s ; Gender:%s ; Age:%s ; Combat effectiveness:%s" % (self.name, self.sex, self.age, self.fight)        #Get current status
        print(temp)          #Print current status
"""Create an instance Atom"""
Atom = Game("Atom","robot",102,500)  #An instance Atom corresponding to the Game class assignment suggestion
Atom.sky()   #Call the method sky through an instance
Atom.sky()   #Call the method sky through an instance
Atom.xinxi() #The result of printing the current status is: Name: Atom; Gender: robot; Age: 102; Combat effectiveness: 700

"""Create an instance Tiga"""
Tiga = Game("Tiga","Ultraman",300,800)  #An instance Tiga corresponding to the Game class assignment suggestion
Tiga.sky()   #Call the method sky through an instance
Tiga.sea()   #Call the method sky through an instance
Tiga.xinxi() #Print the current status result as: Name: Tiga; Type: Altman; Age: 300; Combat effectiveness: 1200

In this example, the class is Game(), and the instance is Atom and Tiga. The instances are independent of each other. The functions called in the instance are all called methods, while in the class they can be called methods or functions. There is only one special difference between the methods of the class and ordinary functions - they must have an additional first parameter name, By convention, its name is self, which represents the instance of the class. This name can also be customized to other names__ init__ () method is a special method called the constructor or initialization method of a class. It will be called when an instance of this class is created

Topics: Python