Home>Article>Backend Development> Python object-oriented video material sharing
Object-oriented is a very mainstream idea in current programming languages. Python has a very good implementation of object-oriented. At the same time, with the help of Python's flexible syntax, some cool object-oriented features can be implemented.
Python has been an object-oriented language from the beginning. Because of this, it is easy to create classes and objects in Python. In this chapter we will introduce object-oriented programming in Python in detail.
If you have not been exposed to object-oriented programming languages before, you may need to first understand some basic features of object-oriented languages and form a basic object-oriented concept in your mind, which will help you Easily learn object-oriented programming in Python.
Next, let’s briefly understand some basic characteristics of object-oriented.
What is difficult to learn about object-oriented is not the concepts and other knowledge points, but the object-oriented way of thinking:
The basic ideas of object-oriented are encapsulation, inheritance, Polymorphic.
First is inheritance:
Define a class:
The code is as follows:
class Bird(object): have_feather = True way_of_reproduction = 'egg'
Call this class:
The code is as follows:
summer = Bird() print summer.way_of_reproduction
Different from Java, Python does not require new to instantiate a class.
Similarly, Python classes can define methods below:
The code is as follows:
class Bird(object): have_feather = True way_of_reproduction = 'egg' def say(self, word='hi hi'): print 'i say :' + word
Note that all class functions must have at least one Parameter, this parameter must be self.
Functions outside the class do not have this restriction.
The code is as follows:
chk = Chicken() print chk.have_feather print chk.sat('hello')
__init__() method
__init__() is a special method. There will be some special methods in Python, and Python will handle them in a special way. Special method names are characterized by two underscores before and after them.
The __init__() method is special in that if you define this method in a class, Python will automatically call this method once you create an object based on this class (this process is also called initialization).
For example:
The code is as follows:
class happyBird(Bird): def __init__(self,more_words): print 'We are happy birds.',more_words hb = happyBird('Happy,Happy!')
Overloading of the parent class method:
The code is as follows:
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__() #由于调用了父类的初始化构造函数,继承了父类的变量的改变 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 allows multiple inheritance, but this is a very dangerous operation and it is recommended not to use it casually.
About Python's polymorphism, just like JavaScript, you can directly access the properties of the object without using interfaces and without type conversion.
For type judgment, there are the type() function and the isinstance() function to judge whether it is a subclass of a certain function.
Copy the code The code is as follows:
isinstance(object, classinfo)
Determine whether the instance is this class or object is a variable
classinfo is a type (tuple, dict, int, float)
Determine whether the variable is of this type
The code is as follows:
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 above is the detailed content of Python object-oriented video material sharing. For more information, please follow other related articles on the PHP Chinese website!