-------------------class properties and instance attribute relationships-------------------
1. Class attributes and Instance attribute relationships
1. Instance Properties
Properties unique to instance objects
2. Class Properties
Class name Access class properties
3. When there is no property of the same name in the instance, the Class property can be accessed and cannot be accessed when the instance property of the same name is defined
4, commonly used to find instructions
1. VARs: View in-instance properties
2. Dir: Show class properties and all instance properties
3. Type: Display Type
-------------------Method-------------------
1. Example method
The implied argument is the class instance self
2. Class methods
The implied argument is the class itself CLS
3. Static method
As an implicit parameter, a static method can also be called directly for a class instance
4. Class names can call class methods and static methods, but cannot invoke instance methods
5. Example:
Class Test (object):
def instancefun (self):
Print ("Instancefun")
Print (self)
@classmethoddef classfun(cls): print("ClassFun") print(cls)@staticmethoddef staticfun(): print("StaticFun")
-------------------Privatization-------------------
1, XX: Public variables
2, _x: Single front underline, privatization properties or methods, 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 (the name is not accessible)
4,xx : Double before and after the underline, the user namespace magical objects or attributes. For example: init, __ do not invent such a name yourself
5, xx__: Single post underline, to avoid the conflict with the Python keyword
6. Example:
#人类
class Person (object):
def Init(self,name,age,state):
Self.name = Name
Self._age = Age
Self.__state = State
def personshow(self): print self.name print self._age print self.__state def _work(self): print(‘in work‘) def __away(self): print(‘in away‘)#学生类继承人class Student(Person): def setInfo(self,name,age,state): self.name = name self._age = age self.__state = state def studentshow(self): print self.name print self._age print self.__state#main入口if __name__ == ‘__main__‘: #创建一个人类的对象 person = Person(‘xiaohao‘, 18, ‘football‘) person.personshow() #创建一个学生对象 student = Student(‘xiaohao‘, 18, ‘football‘) student.setInfo(‘xiaowang‘,25,‘basketball‘) student.studentshow() student.personshow() person._work() person._Person__away()
-------------------Analyze A class-------------------
1.Init
1. Description: Construct initialization function
2, trigger conditions: After creating the instance, the assignment is used, after new
2.New
1. Description: Generate the required properties for the instance
2. Trigger condition: When creating an instance
3.class
1. Description: The class where the instance resides
2. Trigger Condition: Instance class
4.Str
1. Description: Instance string representation, readability
2. Trigger Condition: Print (class instance), if not implemented, use REPR result
5,repr
1. Description: Instance string representation, accuracy
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
1. Description: Instance Custom properties
2. Trigger Condition: VARs (instance dict)
8.Doc
1. Description: Class document, subclass does not inherit
2. Trigger Condition: Help (class or instance)
9,getattribute
1. Description: Property access Blocker
2. Trigger condition: When accessing instance properties, priority is higher than dict access
3. Example:
#coding =utf-8
Class Itcast (object):
def Init(SELF,SUBJECT1):
Self.subject1 = Subject1
Self.subject2 = ' cpp '
#属性访问时拦截器,打log def __getattribute__(self,obj): if obj == ‘subject1‘: print(‘log subject1‘) return ‘redirect python‘ else: #测试时注释掉这2行,将找不到subject2 return object.__getattribute__(self,obj) def show(self): print ‘this is Itcast‘ s = Itcast(‘python‘) print s.subject1 print s.subject2
10,bases
1. Description: All the parent classes of the class constitute elements
2. Trigger conditions:
-------------------Property Protection-------------------
1. Example:
#coding =utf-8
class Man(object): def __init__(self, name, age): self._name = name self._age = age @property def name(self): return self._name @property def age(self): return self._age @age.setter def age(self, age): if not isinstance(age, int): raise ValueError(‘age should be int‘) if age < 0 or age > 150: raise ValueError(‘age should be 0-150‘) self._age = agem = Man(‘jack‘, 32)print(m.name)print(m.age)m.age = 40print(m.age)m.name = ‘rose‘ #此处报错
-------------------Object-oriented design-------------------
1. Package:
2. Inheritance: Class Cat (Animle) class Animle (object)
1. Rewrite
Class Cat (ANIMLE):
Def chi (self):
Rewrite
Animal.chi (self)
3, polymorphic:
1. Virtual method
2. Abstract methods
3. Interface
4. Example:
Class Dog (Animal):
Def chi (self):
print ' Chi '
def test (animal):
Animal.chi ()
Dog = Dog ()
Test (dog)
Python Learning Summary (object-oriented advanced)