When I was a sophomore, I studied Java at school,
The most awesome oo idea, with 3 pages of paper is finished, or Tsinghua University Press.
Later all by themselves Chew video, chew code to understand what is called OO.
Learn Python now and learn it in your own way:
The basic of OO is encapsulation, inheritance, polymorphism.
The first is inheritance:
Define a class:
Python code
- Class Bird (object):
- Have_feather = True
- way_of_reproduction = ' egg '
Call this class:
Python code
- Summer = Bird ()
- Print Summer.way_of_reproduction
Unlike Java, Python does not require new to instantiate a class.
Similarly, the Python class below is a way to fix the method:
Python code
- Class Bird (object):
- Have_feather = True
- way_of_reproduction = ' egg '
- def say (self, word=' Hi Hi '):
- print ' I say: ' + word
Note that functions of all classes must have at least one argument, and this argument must be self.
A function other than a class does not have this restriction.
Python code
- CHK = Chicken ()
- Print Chk.have_feather
- Print Chk.sat (' hello ')
__init__ () method
__init__ () is a special approach (special method). Python will have some special methods, and Python will handle them in a special way. The name of the special method is characterized by a two underscore before and after.
The special of the __init__ () method is that if you define this method in a class, Python automatically calls this method (which is also called initialization) once you have created the object from that class.
Such as:
Python code
- Class Happybird (Bird):
- def __init__ (self,more_words):
- print ' We are happy birds ', more_words
- HB = Happybird (' happy,happy! ')
Overloads of the Parent class method:
Python code
- Class Hello (object):
- name = ' Hello '
- def __init__ (self):
- self.name=' My name is Hello '
- #类中的参数必须带有self参数
- def sayhi (self):
- print ' Hi you '
- Class World (Hello):
- def __init__ (self):
- #这里访问的是父类初始化的变量名
- print ' before: ', Hello.name
- Super (World, Self). __init__ ()
- #由于调用了父类的初始化构造函数, inherits the change of the parent class's variable
- print ' after: ',self.name
- #近似于方法重载
- def sayhi (self,word=' baby '):
- #调用父类sayhi方法
- Super (World, Self). Sayhi ()
- print ' Hi ' +word
- def sayworld (self):
- print ' Hi,hello World '
- if __name__ = = ' __main__ ':
- c = World ()
- C.sayhi ()
- C.sayworld ()
In addition, Python is allowed to inherit multiple, but this is a very dangerous operation, it is recommended not to use it casually.
With regard to Python polymorphism, like JavaScript, direct access to the properties of an object without the need for an interface, no type conversion.
For the type of judgment, there are the type () function of the grab, and the isinstance () function determines whether the subclass of a function.
Python code
- Isinstance (object, ClassInfo)
- Determine if the instance is this class or object is a variable
- ClassInfo is a type (tuple,dict,int,float)
- Determine if the variable is of this type
- Class Obja:
- Pass
- A = Obja ()
- B = ' A ',' V '
- C = ' A string '
- Print isinstance (A, Obja)
- Print isinstance (B, tuple)
- Print isinstance (C, basestring)
- Output Result:
- True
- True
- True
The OO idea of Python