-------------------class properties and instance attribute Relationships-------------------1, class properties and instance attribute relationships 1, instance properties Instance object Exclusive Properties 2, class properties Class name Access class properties 3, instances without the same name attribute, access to class properties, cannot be accessed when defining instance properties of the same name 4, common find Directives 1, VARs: View in-instance properties 2, dir: Show class properties and all instance properties &N Bsp 3, type: Display Type -------------------method-------------------1, instance method Implied argument is class instance self 2, Class methods implied arguments for the class itself cls 3, static methods as implicit parameters, primarily for class instances can also call static methods directly 4, class names can call class methods and static methods, but can not invoke instance methods 5, Example: Class Test (object): def instancefun (self): print ("Instancefun") print (self) @classmethod def classfun (CLS): &nbs P Print ("Classfun") print (CLS) @staticmethod DEF Staticfun (): print ("Staticfun") -------------------Privatization-------------------1, XX: public variable 2, _x: Single front underline, privatization attribute or method, from somemodule import * Prohibit import, class objects and subclasses can access 3, __XX: Double-front underline, avoid naming conflicts with attributes in subclasses, cannot be accessed directly outside (name reorganization is not accessible) 4, __xx__: Double front and back underline, User namespace Magic object or attribute. For example: init, __ do not invent the name yourself 5, xx__: Single-post underline to avoid conflicts with Python keywords 6, sample: #人类 class person ( Object): def __init__ (self,name,age,state): Self.name = name Self._age = age Self.__state = s tate def personshow (self): Print self.name print self._age Print self.__state   ; def _work (self): print (' on work ') def __away (self): Print(' in Away ') #学生类继承人 class Student (person): def setInfo (Self,na Me,age,state): Self.name = name SELF._AG E = age self.__state = state def studentshow (sel f): print self.name Print self._age  ; print self.__state #main入口 if __name__ = = ' __main__ ': &N Bsp #创建一个人类的对象 person = person (' Xiaohao ', ' Football ') & nbsp Person.personshow () #创建一个学生对象 student = student (' Xiaoh Ao ', ' Football ') student.setinfo (' Xiaowang ', +, ' basketball ') & nbsp STUDEnt.studentshow () student.personshow () Person._work ( ) Person._person__away () -------------------analyze a class-------------------1, __init __ 1, Description: Construct the initialization function 2, Trigger condition: After creating the instance, use after new, 2, __new__ 1, Description: Generate the properties required by the instance & nbsp 2. Trigger condition: 3, __class__ 1, Description: Instance's class 2, Trigger condition: instance __class__ 4, __str__ 1 when creating instance , Description: Instance string representation, readability 2, Trigger Condition: print (class instance), if not implemented, using REPR results 5, __repr__ 1, Description: Instance string representation, accuracy & nbsp 2. Trigger Condition: class instance carriage return or print (Repr (class instance)) 6, __del__ 1, Description: Destruction 2, Trigger condition: Del Delete instance 7, __dict__& nbsp 1, Description: Instance custom properties 2, Trigger condition: VARs (instance dict) 8, __doc__ 1, Description: Class document, subclass does not inherit 2, Trigger condition: H ELP (class or instance) 9, __getattribute__ 1, Description: Property access blocker 2, Trigger condition: When accessing instance properties, priority is higher than __dict__ access 3, instance: #coding=utf-8 class Itcast (object): DEF __i nit__ (Self,subject1): Self.subject1 = subject1 SELF.SUBJECT2 = ' cpp ' &NBS P #属性访问时拦截器, play log def __getattribute__ (self,obj): if obj = = ' Subject1 ': &NB Sp print (' Log subject1 ') &NB Sp return ' redirect Python ' else: #测试时注释掉这2行 will not find subject2 &NBS P return object.__getattribute__ (self,obj) def show (self): print ' This is Itcast ' s = itcast (' python ') print s.subject1 print s.subject2 10, __bases__& nbsp 1, Description: All parent classes of the class constitute elements 2, trigger conditions: ------------------- Property protection-------------------1, instance: #coding =utf-8 Class Man (object): def __init__ (self, name, age): Self._name = name &NBSP ; Self._age = age @property def name (self): return self._name @property def (self): &NBSP; Return self._age @age .setter def age (self, age): If isinstance (age, int): raise Valu Eerror (' age should is int ') if age < 0 or age > 150: &N Bsp raise ValueError (' age should is 0-150 ') Self._age = a ge m = Man (' Jack ', +) print (m.name) print (m.age) M.age = 40&nbs P Print (m.age) M.name = ' Rose ' #此处报错 -------------------Object-oriented design-------------------1, Package: 2 , Inheritance: Class Cat (Animle) class Animle (object) 1, rewrite class Cat (ANIMLE): &N Bsp def chi (self): rewrite & nbsp &NBsp Animal.chi (self) 3, polymorphic: 1, virtual method 2, abstract method 3, interface 4, example: & nbsp class Dog (Animal): def chi (self): print ' chi ' DEF test (animal): an Imal.chi () dog = Dog () test (dog)
Python Learning Summary (object-oriented advanced)