I hope the basics of the previous two Pyhon are a little bit more for newcomers to Python, and then I'll write object-oriented knowledge.
First of all, let's say inherit. What is inheritance, the simple point is that the son inherits the property of Lao Tzu, for example, we created a people class with name, age, etc.
Then we're creating a student class that has Stuid, class, and so on. We let student inherit people, so we can call student when we call people
The attributes inside.
The code is as follows:
classpeople:def __init__(self,name,age): Self.__dict__["name"]=name self.__dict__[" Age"]= Agedef __setattr__(self, Key, value): Self.__dict__[key]=valuedef __getattr__(self, item):returnSelf.__dict__[item]defShow (self):Print("my name is.", Self.name)#Student class inheritance PeopleclassStudent (People): a=0#This is the static property . def __init__(Self,name,age,id,stuclass): Super ().__init__(Name,age) self.__dict__["ID"] = ID#Instance PropertiesSelf.__dict__["Stuclass"] =Stuclassdef __setattr__(self, Key, value): Self.__dict__[key]=valuedef __getattr__(self, item):returnSelf.__dict__[item]defShow (self): Super (). Show ()Print("my age is", self.__dict__[" Age"]) s=student ("Hans"21st"12121", 2)#instantiation of a classPrint(S.name)#output HansS.show ()#executes the parent class's method of overriding the parent class in the child class
1. Inheritance
The above code such as student inherited the people, so student can call the people inside the properties and methods, the above code is labeled, it is not to be exhausted.
2. Instantiation
In the above code s=student () This is the instantiation of the class, the parentheses are written in the specific parameters to be passed
3. Access modifiers (no direct access modifier)
Public: Any method can be called
Private: Only You can access the __XXX (add two underscores to the front of the attribute)
proctected protection: only own and subclass accessible _xxx (preceded by an underscore in the attribute)
4. Static members
For example, in the above code, a is a static property
Static methods S=student () student.show (s) static methods are called directly with the class name and need to pass in an instance
5. Get and set for attributes inside a class
Rewrite GetAttr and setattr directly in Python in the previous year's code
Ok this article is here, if there are errors please leave a message, thank you:)
One of Python's advanced steps