1. The concept and structure of class
1) The concept of class
Like other languages, classes in python are collections used to describe objects with the same properties and methods. It defines the properties and methods that are common to each object in the collection. An object is an instance of a class.
For example, the concept of human is a class. As long as it is human, there are some attributes (height, weight, gender, etc.) and methods (eating, drinking, running and jumping, etc.).
More specifically, Xiao Hong and Xiao Ming are both examples of human beings. Xiao Hong and Xiao Ming both have height, weight and gender, and they also eat, drink, run and jump. But Xiaohong and Xiaoming must have different values of height, weight, gender, eating, drinking, running and jumping.
2) Class definition and instantiation
#Class definition class people: #Define basic properties name = '' age = 0 #Define private property, which cannot be accessed directly outside the class __weight = 0 #Defines a constructor that is called automatically when the class is instantiated def __init__(self,n,a,w): self.name = n self.age = a self.__weight = w def speak(self): print("%s say: I %d Year old." %(self.name,self.age)) # Instantiated class XiaoHong = people('XiaoHong',16,35) # Assign a value to a property by constructing a method XiaoMing = people('XiaoMing',18,65) # Access attribute print(XiaoHong.name) print(XiaoMing.age) # Method of calling instance XiaoHong.speak() XiaoMing.speak()
In the above code, there is a special method in the class called the constructor. This method will be executed first when the object is instantiated, so it can be used to assign values to the properties of the object.
In addition, there is a special difference between the methods of a class and ordinary functions - they must have an extra first parameter name, which is conventionally self. Self represents an instance of the class, represents the address of the current object, and self.class points to the class.
2. Class inheritance
1) Class inheritance
#Class definition class people: #Define basic properties name = '' age = 0 #Define private property, which cannot be accessed directly outside the class __weight = 0 #Define construction method def __init__(self,n,a,w): self.name = n self.age = a self.__weight = w def speak(self): print("%s say: I %d Year old." %(self.name,self.age)) #Single inheritance example class student(people): grade = '' def __init__(self,n,a,w,g): #Call the constructor of the parent class people.__init__(self,n,a,w) self.grade = g #Method of overriding parent class def speak(self): print("%s say: I %d Age, I'm reading %d grade"%(self.name,self.age,self.grade)) s = student('ken',10,60,3) s.speak()
2) Class multiple inheritance
#Class definition class people: #Define basic properties name = '' age = 0 #Define private property, which cannot be accessed directly outside the class __weight = 0 #Define construction method def __init__(self,n,a,w): self.name = n self.age = a self.__weight = w def speak(self): print("%s say: I %d Year old." %(self.name,self.age)) #Single inheritance example class student(people): grade = '' def __init__(self,n,a,w,g): #Calling the constructor of the parent class people.__init__(self,n,a,w) self.grade = g #Method of overriding parent class def speak(self): print("%s say: I %d Age, I'm reading %d grade"%(self.name,self.age,self.grade)) #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("My name is %s,I'm a speaker. The theme of my speech 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. By default, the method of the parent class before the bracket is called
3, Method override
1) Override method in parent class
class Parent: # Defining parent class def myMethod(self): print ('Call parent method') class Child(Parent): # Defining subclasses def myMethod(self): print ('Call subclass method') c = Child() # Subclass instance c.myMethod() # Subclass call override method, the result is: call subclass method # super function is a method used to call the parent class super(Child,c).myMethod() #Call a method whose parent class has been overridden with a subclass object, and the result is: call the parent class method
2) Subclass inheritance parent constructor description
① the subclass does not override the init. When instantiating the subclass, the init defined by the parent class will be called automatically__
class Father(object): def __init__(self, name): self.name=name print ( "name: %s" %( self.name) ) def getName(self): return 'Father ' + self.name class Son(Father): def getName(self): return 'Son '+self.name son=Son('LC') print ( son.getName() ) # The output is # name: LC # Son LC
(2) if you override init, instantiate the subclass, and the defined init of the parent class will not be called__
class Father(object): def __init__(self, name): self.name=name print ( "name: %s" %( self.name) ) def getName(self): return 'Father ' + self.name class Son(Father): def __init__(self, name): print ( "hi" ) self.name = name def getName(self): return 'Son '+self.name son=Son('LC') print ( son.getName() ) # The output is # hi # Son LC
(3) if you override ﹣ init ﹣ and want to inherit the construction method of the parent class, you can use the super keyword
class Father(object): def __init__(self, name): self.name=name print ( "name: %s" %( self.name)) def getName(self): return 'Father ' + self.name class Son(Father): def __init__(self, name): super(Son, self).__init__(name) print ("hi") self.name = name def getName(self): return 'Son '+self.name son=Son('runoob') print ( son.getName() ) # The output is # name: LC # hi # Son LC
4. Private property and private method of class
As mentioned before, a class contains properties and methods. When a class is instantiated, the call can be accessed by instance name. Property name or instance name. Method name.
In addition, private properties and private methods will appear in the class. As the name implies, properties and methods can only be used inside a class. Cannot be used or directly accessed outside of a class.
Private property of a class: starts with two underscores and is used in methods inside the class.
Class's private methods: start with two underscores and use them in methods inside the class.
class JustCounter: __secretCount = 0 # private variable publicCount = 0 # Open variable def count(self): self.__secretCount += 1 self.publicCount += 1 print (self.__secretCount) class Site: def __init__(self, name, url): self.name = name # Public attribute self.__url = url # Private attributes def who(self): print('name : ', self.name) print('url : ', self.__url) def __foo(self): # Private method print('This is a private method') def foo(self): # Public method print('This is a public method') self.__foo() counter = JustCounter() counter.count() counter.count() print (counter.publicCount) print (counter.__secretCount) # Error reported, instance cannot access private variable x = Site('LC', 'liuchengblog.github.io') x.who() # Normal output x.foo() # Normal output x.__foo() # Report errors