Class attribute instance variable: __init__ the variable that needs to be initialized, the scope of the instance variable is the instance itself class variable: A variable private property written in class Global, as follows: This property cannot be called by an instance. 。
When you want to access a private property, you can create a new method, the internal method can call the private property, and then call this method to get the value of the private property:
Print (R1.show_status ())
If an instance invokes a variable, the instance variable and the class variable all define the same variable, then the instance variable is prioritized and no class variable is found.
methods structuring methods: &NBS P def __init__ (self): Self.name = name destructors, which are automatically executed when the instance is freed (delete instance del or instance running), usually do some finishing work. Such as: Close some database links, close open temporary files def __del__ (self): do something Private method, set The normal method is preceded by two underscores objects Instantiate a class and get the object package The implementation details of some features are not exposed first-level encapsulation (Nothing): Creating classes and objects creates namespaces for both, and we can only use the class name. or obj to access the name inside, which is itself a package. Second-level encapsulation: The class hides certain properties and methods (or defines them as private), is used only inside the class, is inaccessible externally, or leaves a small number of interfaces (functions) for external access. inheritance code reuse single inheritance multiple inheritance 2.7 Classic Class (Class people ()), depth first, new class people (object), breadth first 3.x breadth First inheritance Examples:
classPeolpe ():def __init__(self,name,age): Self.name=name Self.age= AgedefTalk (self):Print("%s is talking ..."%self.name)defSleep (self):Print("%s is sleeping ..."%self.name)classMan (Peolpe):##此处继承 PassM1= Man ("Liyang", 22) M1.sleep ()
Inheritance Reuse
classPeolpe ():def __init__(self,name,age): Self.name=name Self.age= AgedefTalk (self):Print("%s is talking ..."%self.name)defSleep (self):Print("%s is sleeping ..."%self.name)classMan (Peolpe):##此处继承 defEat (self):Print("%s is eating"%self.name)defSleep (self): Peolpe.sleep (self)##此处调用父类方法, if there is no line, the sleep method overrides, and the parent class method is not executed Print("Man also is sleeping") M1= Man ("Liyang", 22) M1.sleep ()
If the subclass wants to add initialization parameters, modify the class
class # #此处继承 def __init__ (self,name,age,location): People. __init__ # #继承父类初始化参数 self.location = Location ##新加一个参数
Or
class # #此处继承 def __init__ (self,name,age,location): # people.__init__ (self,name,age) # #继承父类初始化参数 Super (Man,self). __init__ (name,age) # #使用super内置函数, new style. self.location = Location ##新加一个参数
Multiple inheritance:
#-*-coding:utf-8-*-#Author:brownyangyangclasspeople ():def __init__(self,name,age): Self.name=name Self.age= AgedefTalk (self):Print("%s is talking ..."%self.name)defSleep (self):Print("%s is sleeping ..."%self.name)classRelation (object):defmake_friends (self,obj):Print("%s is makeing friends with%s"%(self.name,obj.name))##self. Name is inherited by people, and obj is an instance of the definition .classMan (people,relation):##此处继承 def __init__(self,name,age,location):#people.__init__ (self,name,age) # #继承父类初始化参数Super (Man,self).__init__(name,age) self.location= Location##新加一个参数 defEat (self):Print("%s is eating"%self.name)defSleep (self): People.sleep (self)##此处调用父类方法, if there is no line, the sleep method overrides, and the parent class method is not executed Print("Man also was sleeping in%s"%self.location)classWoman (people,relation):defGet_birth (self):Print("%s is borning the baby"%Self.name) M1= Man ("Xiaohong", 22,"Nantong")#M1.sleep ()W1= Woman ("xiaoming", 32)#W1.get_birth ()M1.make_friends (W1)
Results: Xiaohong is makeing friends with Xiaoming
Multi-state interface reuse, an interface multiple implementation method to modify the static method @staticmethod is only the nominal classification of the tube, in fact, it is not related to the class, in fact, in the static method can not access the class or any attribute class method in the instance @classmethod Only class variables can be accessed, instance variable property methods cannot be accessed @property a method into a static property
Python Object-oriented