The characteristics of object-oriented and its own sentiment
1._init_ is a constructor that assigns a value to an instance variable (static variable assignment, property)
2. Selfrepresents the passing of an instance, and whoever invokes it will send it in .
3. instance variables in the constructor, in the memory space of the instance
4. A method in a class, a variable of a class. exists in the memory space of the class
5. class variables are useful for all instances (objects), but when the same name is first given as the instance variable, it is created only once in memory, saving overhead
6. The instance variable can be added (object . Added property = value), modified (object. property name = value), delete (Del object. Properties)
7. modifying a class variable in an object will only have an effect on the current object. The class variables in other objects do not change. The reason is that a class variable belongs to a class and belongs to all objects. The change in the current object is equivalent to Adding an attribute with the same name as the class variable to the instance. So although the same name, but read is not a class variable.
8. If a class variable is a data structure such as a list or a tuple dictionary Collection , add elements to different objects Or something else, it will change the list. Because he does not exist in the class, and there is no instance. Other functions have the same effect on this data structure.
9. the operation that the destructor uses to wrap up is called when memory is reclaimed, by default, when the program ends. You can also delete the del object voluntarily, and then he will execute it after all the operations of this object have been completed.
def __del__ (self):
Pass
private properties. Method. Add two _ in front of it. Access can only be made within the class and cannot be read directly from outside. This avoids the direct passage of a class, or object. Obtain or modify data. Data becomes more secure.
root cause of private property unreachable is that its name has changed.
becomes the _ class name __ variable name
Inherited
Refactoring a parent class method
Subclass adds a new property
another way to call the parent class Super
Two kinds of search strategies for instance variables
Example: A is inherited by B C, and D inherits B C
A
B (a) C (a)
D (B,C)
Breadth First Order D>b>c>a
Depth First Order D>b>a>c
Example
#父类1
classPeople (object):
def__init__ (self,name,age):
Self.name=name
Self.age=age
defETA (self):
Print"%s is eating bitter gourd"%self.name)
#父类2
classWork (object):
defPapa (self):
Print"%s is knocking code"%self.name)
#继承两个父类
classHouzi (people,work):
#添加一个新属性city
def__init__ (self,name,age,city):
people.__init__ (Self,name,age)
Self.city=city
defChengshi (self):
Print"%s lives at%s"% (self.name,self.city))
defETA (self):
"Refactoring the Parent class method"
Print"%s is the wisdom tooth."%self.name)
People.eta (self)
#创建一个实例
H1=houzi ("Sledgehammer","All","Beijing")
H1.eta ()
H1.papa ()
H1.chengshi ()
Output
Polymorphic
Same interface, multiple implementations
Python Learning notes--LU