Python Object-oriented programming

Source: Internet
Author: User


I. Terminology
Object: an instance created from a template that can invoke functions in an execution class through an instance object
Class: A class is a template defined by the keyword class that contains multiple functions that implement a series of functions.
Polymorphism: The literal meaning of polymorphism is a variety of forms. In the practical case of programming: The program receives 1 objects, but does not understand how the objects are implemented internally
Encapsulation: Encapsulation is not equal to polymorphism, and encapsulation is the principle that points to the specific implementation details of hidden objects in other parts of the program.
Inheritance: Inheritance simply means to inherit the content of the parent class from the real life neutron class. In Python, which is based on subclasses and parent classes, subclasses have properties and methods of the parent class.

Ii. differences in polymorphism and encapsulation
1. Encapsulation can be used directly without caring about how the object is built.
2. Polymorphism allows the user to make method calls to objects that do not know what the class is.

III. Classes and objects
1. Methods for creating classes and objects:
 #这里创建一个类, the class name is person, with the method walk () and say () Class Person: #class class name def walk (self): #定义方法walk (): def function 1 (parameter 1): statement 1 print "I can walk quickly" def Say (self,name): #定 Semantic method Say (): def function 1 (parameter 1): statement 1 print "Hello,i am%s"%name jachy = Person () #用Person类创建1个对象jachy: Object name = Class Name () Jachy.walk () #jachy对象调用walk () method Jachy.say (' Jachy ') #jachy对象调用say () method 
  note:  
1. Functions in a class we call methods Br> 2. The first argument to a function in a class must be self


1. Object-oriented: Create Object---> Execute method via object
2. function Programming: Execute function

four, object-oriented three major features: encapsulation + inheritance + polymorphism
1. Encapsulation, in a nutshell, encapsulates attributes in one place to facilitate subsequent invocation of encapsulated properties
--------> Package First
Class Person:            def __init__ (self,name,age):  #这个__init__ () is called the constructor method, when the class creation object is automatically executed with this method                Self.name = name  # The name passed in is encapsulated in the self in                self.age =            age #再将传进来的age封装到self中 #这里self是形参,            #当执行下面的obj1 = person (' jachy ', 21), Self is equivalent to obj1            #当执行下面的obj1 = person (' Lilin ', 22) when self is equivalent        to the Obj2 #根据类Person创建对象obj1,        # The __init__ method in the class person is automatically executed at the same time as created        obj1 = person (' jachy ', +)  #将jachy和21封装到obj1对象汇总        obj2 = person (' Lilin ') #将lilin和22封装到obj2对象汇总        #两个属性就被封装到obj1和obj2中, the            object obj1 has name= ' jachy ' and age=21, enclosing name and age into the object in memory. The            object obj2 has name= ' Lilin ' and age=22, which encapsulates name and age into the object in memory.
        -----> Re-call:
The call is divided into 2 kinds: 1, direct call through the object 2. Indirectly via self
1. Indirect invocation via Self
Class Person:                def __init__ (self,name,age):                    self.name = name                    Self.age = Age                def message (self):                    The self                    parameter in print self.name print self.age                #python内部会将obj1传给Person (), which is self = obj1                obj1 = person (' jachy ', 21)                Obj1.message ()
            2. Direct invocation via Object
Class Person:                def __init__ (self,name,age):                    self.name = name                    Self.age = Age                Obj1=person (' Jachy ', )                print obj1.name  #直接调用obj1对象的属性name                print obj1.age   #直接调用obj1对象的属性age
 
2. Inheritance, simply said that inheritance is the real life of the father and child inheritance of the relationship, that is, the definition of 1 subclasses, subclasses can use the parent class defined methods and properties, this process is inherited
---> Inherit template 1: Single inheritance
          Class Parent class Name: Def func1 (self): Pass class subclass Name (parent class name):            Pass #创建子类对象 sub = Subclass name () #执行从父类继承的方法 sub.func1 () Class Animal:def Eat (self): print '%s eat '% (self.name) def                    Sleep (self): print '%s sleep '% (self.name) #定义子类Cat class Cat (Animal):                    def __init__ (self,name): self.name = name self.sth= ' Cat ' Def cry (self): print "Miao--miao" #定义1个子类Dog class Dog (Ani MAL): def __init__ (self,name): Self.name = name Self.sth = "Dog" def Cry (self): print "Wang--wang" #真正执行 c1 = Cat (' Black-cat ') c1.eat () C2 = Dog (' White-dog ') c2.eat () 

    ---> Inheritance template 2: Multiple inheritance (divided into classic inheritance and modern inheritance)
       Class D: #这种类型就是经典类 def bar (self): print ' D.bar ' class C (d):                    def bar (self): print ' C.bar ' class B (D): Def bar (self):                    print ' B.bar ' class A (b,c): #最终调用bar的查找顺序: A--b--d---C def bar (self): print ' a.bar ' a = A () #首先会执行bar () method, and then it will be related to several classes to find, once found to stop the search immediately, if not found, the error #然后第一去A类中查                    Find, Class A does not have bar method, then go to Class B; If class B does not have bar method to go to Class D; If Class D is not the last to go to Class C (object): #这种类型就是新式类 def Bar (self):            print ' D.bar ' class C (D): Def bar (self): print ' C.bar ' Class B (D): Def bar (self): print ' B.bar ' class A (b,c): Def B AR (self): print ' a.bar ' c = A () #首先执行bar方法, then go to the class to find #因为此类是新式类, look in order: a--            -B----C---DA.bar () 
Classic class Lookup Order: A---B---D---C (depth first find policy)
New Class Lookup Order: A---B----C----D (breadth first find policy)

Python Object-oriented programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.