Python-an object-oriented language with the largest characteristics of encapsulation, inheritance, and polymorphism.
Encapsulation: Encapsulation can be used directly without caring about how the object is built. Is the principle of hiding excess information from other areas of the global scope
To define a class:
1>>>classPerson ():2 defSetName (self,name):3Self.name =name4 defGetName (self):5 returnSelf.name6 defgreet (self):7 Print 'Hello i am%s'% Self.name
Self: is a reference to the object itself, the difference between a method (a more specialized point, which can be called a binding method), and a function
1 >>> foo = person () 2 >>> bar = person () 3 >>> foo.setname (" Zhang " ) 4 >>> bar.setname ( " di Span style= "COLOR: #800000" > " ) 5 >>> Foo.greet () 6 hello I am Zhang 7 >>> Bar.greet () 8 hello i am di
Features can be accessed externally
1 >>> foo.name2'Zhang'3 >>> Bar.name4'di'5'lu' 6 >>> bar.greet ()7 Hello I am lu
Private methods, outside the class can not be called, internal callable, with "__" start
1 classService:2 def __a(self):3 Print "You can ' t see me"4 5 defB (self):6 Print "You can see me"7Self.__a()8 9>>> s =Service ()Ten>>> S.__a() One A Traceback (most recent): -File"<pyshell#29>", Line 1,inch<module> -S.__a() theAttributeerror:service instance has no attribute'__a' ->>>s.b () - You can see me -You can't See me
The definition of a class is actually a block of code execution
1 class A: 2 Print " A is created " 3 4 5 is created
Note the difference between the following code
1>>>classMemberCount ():2Members =03 definit (self):4Self.members + = 15 7>>> M1 =MemberCount ()8>>>M1.init ()9>>>m1.membersTen1 One>>> m2 =MemberCount () A>>>M2.init () ->>>m2.members -1 the>>>classMemberCount (): -Members =0 - definit (self): -Membercount.members + = 1 + +>>> M1 =MemberCount () A>>>M1.init () at>>>m1.members -1 ->>> m2 =MemberCount () ->>>M2.init () ->>>m2.members -2 in>>>m1.members -2
To specify a super class
1>>>classFilter:2 definit (self):3self.blocked = []4 defFilter (self,sequence):5 return[x forXinchSequenceifX not inchself.blocked]6 7 8>>>classSapmfilter (Filter):9 definit (self):Tenself.blocked = ['SPAM'] One A ->>> s =Sapmfilter () ->>>S.init () the>>> S.filter (['SPAM','EFF','A','SPAM'])
Investigate inheritance to see if a class is a subclass of another class, you can use the built-in Issubclass function
1>>>Issubclass (sapmfilter,filter)2 True3>>>Issubclass (filter,sapmfilter)4 False5 #to view the parent class of a known class6>>> Sapmfilter.__bases__7(<class __main__. Filter at 0x0299b0d8>,)8>>> s =Sapmfilter ()9 #to see if an object is an instance of a classTen>>>isinstance (s,sapmfilter) One True A>>>isinstance (s,filter) - True ->>>isinstance (S,STR) theFalse
Multiple superclass multiple inheritance is minimized, and if a method inherits from multiple superclass (that is, two superclass have different methods with the same name), the method in the inheriting class will override the method in the inheriting class.
1>>>classA:2 defAA (self):3 Print "This is a"4 5 6>>>classB:7 defBB (self):8 Print "This is b"9 Ten One>>>classC (A, b): A Pass - ->>> cc =C () the - ->>>Cc.aa () -This isa +>>>cc.bb () -This isb + A>>> c.__bases__ at(<class __main__. A at 0x0299b148>, <class __main__. B at 0x0299b1b8>)
Python Learning 6-objects