Python OO and PythonOO
I want to learn Java at school in my sophomore year,
The most awesome OO thought was finished on three pages, or from Tsinghua University Press.
Later, I learned what OO is based on my own video and code.
Now, you can learn Python in your own way:
OO is basically encapsulation, inheritance, and 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 need new to instantiate classes.
Similarly, Python classes can be set as follows:
Python code
- Class Bird (object ):
- Have_feather = True
- Way_of_reproduction = 'egg'
- Def say (self, word = 'hihi '):
- Print 'I say:' + word
Note that all class functions must have at least one parameter, which must be self.
Functions other than classes do not have this restriction.
Python code
- Chk = Chicken ()
- Print chk. have_feather
- Print chk. sat ('hello ')
_ Init _ () method
_ Init _ () is a special method (special method ). Python has some special methods. Python will process them in a special way. The name of a special method is characterized by two underscores.
The special feature of the _ init _ () method is that if you define this method in the class, once you create an object based on this class, python will automatically call this method (this process is also called initialization ).
For example:
Python code
- Class happyBird (Bird ):
- Def _ init _ (self, more_words ):
- Print 'we are happy birds. ', more_words
- Hb = happyBird ('Happy, Happy! ')
Overload of the parent method:
Python code
- Class Hello (object ):
- Name = 'hello'
- Def _ init _ (self ):
- Self. name = 'My name is hello'
- # The parameters in the class must contain the self Parameter
- Def sayhi (self ):
- Print 'Hi you'
- Class World (Hello ):
- Def _ init _ (self ):
- # The variable name initialized by the parent class is accessed here.
- Print 'before: ', Hello. name
- Super (World, self). _ init __()
- # Because the initialization constructor of the parent class is called, the variable changes of the parent class are inherited.
- Print 'after: ', self. name
- # Similar to method overload
- Def sayhi (self, word = 'baby '):
- # Call the sayhi method of the parent class
- 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 allows multi-inheritance, but this is a very dangerous operation. We recommend that you do not use it at will.
For Python polymorphism, just like JavaScript, you can directly access object attributes without using interfaces or type conversion.
For the type judgment, there are the type () function, andIsinstance ()Determines whether a function is a subclass.
Python code
- Isinstance (object, classinfo)
- Determines whether the instance is a class or an object is a variable.
- Classinfo is a type (tuple, dict, int, float)
- Determines whether 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