Python class definition

  • 2020-04-02 13:03:43
  • OfStack

I. class definition:

class < The name of the class >:
 < statements >

Once a class is instantiated, its properties can be used, and in fact, once a class is created, its properties can be accessed by the class name. If you modify its properties directly with the class name, it directly affects the object that has been instantiated

Private properties of a class:
__private_attrs   Two underscores begin, declaring that the property is private and cannot be used outside the class or accessed directly. Use self.arbitration _attrs in methods within the class
Methods of a class
Inside the class, you can define a method for the class using the def keyword. Unlike normal function definitions, the class method must contain the parameter self as the first parameter
Private class methods
_private_method starts with two underscores and declares that the method is private and cannot be called outside of the class. Call slef. arbitration _methods within the class

Class proprietary methods:
__init__   Constructor, called when the object is generated
__del__     Destructor, used when releasing an object
S print and convert
S/s by index
S/s get the value by index
S get the length
S/p comparison operation
S/s function call

__add__ add operation
__sub__ subtraction operation
__mul__ multiplication operation
__div__ divide
S. S. S. S. S. S
__pow__ said party

# The class definition   
    class people:  
        # Define basic properties   
        name = ''  
        age = 0  
        # Define private properties , Private properties cannot be accessed directly outside the class   
        __weight = 0  
        # Define the constructor   
        def __init__(self,n,a,w):  
            self.name = n  
            self.age = a  
            self.__weight = w  
        def speak(self):  
            print("%s is speaking: I am %d years old" %(self.name,self.age))  

      
    p = people('tom',10,30)  
    p.speak()

Ii. Definition of inheritance class:
1. The single inheritance

class < The name of the class >( The parent class name )
   < statements >


class childbook(book)
    age = 10


# Single inheritance example   
    class student(people):  
        grade = ''  
        def __init__(self,n,a,w,g):  
            # Invokes the construct of the parent class   
            people.__init__(self,n,a,w)  
            self.grade = g  
        # Overrides the method of the parent class   
        def speak(self):  
            print("%s is speaking: I am %d years old,and I am in grade %d"%(self.name,self.age,self.grade))       

    s = student('ken',20,60,3)  
    s.speak()

2. Multiple inheritance of classes

class  The name of the class ( The parent class 1, The parent class 2,...., The parent class n)
     < statements 1>

Note the order of the parent classes in parentheses. If the parent class has the same method name and the child class is not specified, python searches from left to right, that is, if the method is not found in the child class, it looks from left to right to see if the parent class contains the method

# Another class, preparation before multiple inheritance   
class speaker():  
    topic = ''  
    name = ''  
    def __init__(self,n,t):  
        self.name = n  
        self.topic = t  
    def speak(self):  
        print("I am %s,I am a speaker!My topic is %s"%(self.name,self.topic))  

# Multiple inheritance   
class sample(speaker,student):  
    a =''  
    def __init__(self,n,a,w,g,t):  
        student.__init__(self,n,a,w,g)  
        speaker.__init__(self,n,t)  

test = sample("Tim",25,80,4,"Python")  
test.speak()# The method name is the same, and by default it calls the method of the parent class before the parenthesis 

Related articles: