Python 3.6 Defining methods and attributes in a class

#Python3 class encapsulation, constructor definition, instantiation attributes, and common attributes

#Abstract a class of things with the same characteristics into a class

#1, class definition keyword: Class

#Classes and methods are named according to the camel case naming rules, but the first letter of the class is capitalized, and the first letter of the method is lowercase
class Animal(object): #This defines an Animal class that inherits the object base class
    pass


#Note that any class will inherit the object base class, so when defining a class, object can be writable or not

# When using a class, you need to instantiate it first



an=Animal() #Instantiate the class Now the an object gets all the methods and properties of Animal


#2, learning of constructors in classes

class Animal1(object): #This defines an Animal class that inherits the object base class
    def __init__(self): #This is the construction method, the parameter self must be added to the construction method
        print('As long as the object is instantiated, I will be called, you can understand me as an initialization method')


a=Animal1() #Instantiate the Animal1 class and automatically output the constructor

#When instantiating the construction object, the construction method must be called. The construction method can only be written as __init__

#If no constructor is defined when the class is declared, the constructor of the parent class will be called by default

# The self parameter in __init__(self) means to instantiate the object itself

'''
#When an object is instantiated and a method in a class is called, a parameter will be passed by default. This default parameter is to pass itself in.
#So only methods with (self) parameters in the class can be called by the instance,
#If the method in the class has no self parameter,
#Then this method can only be called by the class, not by the instantiation of the class
'''


#properties, instantiation properties, and class properties
#Class attributes are also called common attributes
class Animal2(): #Define an Animal2() class, although no object is written, it inherits by default
    def __init__(self,name): #Define the constructor
        self.name=name #This is called the instantiation attribute defined in the method is called the instantiation attribute

    def getName(self):
        print(self.name) #ask why the name attribute is not defined in my getName, I can output
                             #Because, the class gets its own name attribute when it is instantiated, and we put the self object in and we have the name attribute
#class properties

class Animal3(): #Define an Animal3() class
    age=2 #This age is a common attribute
    def __init__(self,name): #Define the constructor
        self.name=name #This is called the instantiation attribute defined in the method is called the instantiation attribute

    def getName(self):
        print(self.name)  

'''
When a shared property is instantiated and called, the shared property will become an instantiated property, remember

'''






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325801193&siteId=291194637