Python Object-oriented programming

Source: Internet
Author: User

1. Process-oriented programming
In the order of functions as units, a set of functions are executed

#Process orientedlang1={'name':'C + +','score':'9.0'}lang2={'name':'Python','score':'8.5'}deflanginfo (lang):Print '%s:%s'% (lang["name"],lang["score"]) langinfo (lang1) langinfo (lang2)

Operation Result:

2. Object-Oriented Programming
Objects have properties and methods, in object units

#Object OrientedclassLang (object):def __init__(Self,name,score): Self.name=name Self.score=scoredefLanginfo (self):Print '%s:%s'%(self.name,self.score) lang1=lang ('C + +','9.0') Lang2=lang ('Python','8.5') Lang1.langinfo () Lang2.langinfo ()

Operation Result:

3. Encapsulation
objects can provide properties and methods for external invocation, hiding internal implementation details, and private information.
Private variable: Two underscore before attribute name

classLang (object):def __init__(Self,name,score): Self.__name=name Self.score=scoredefLanginfo (self):Print '%s:%s'% (self.__name, Self.score) lang1=lang ('C + +','9.0') Lang2=lang ('Python','8.5')PrintLang1.scorePrintLang1.__name

Operation Result:

As you can see, __name is a private variable (private) and score is a public variable
So how do I change the value of a private variable?
Set and get the value of a private variable through the public method:

classLang (object):def __init__(Self,name,score): Self.__name=name Self.score=scoredefLanginfo (self):Print '%s:%s'% (self.__name, Self.score)defSetName (self,name): Self.__name=namedefGetName (self):PrintSelf.__namelang1=lang ('C + +','9.0') Lang2=lang ('Python','8.5') Lang1.setname ('C') Lang1.getname ()

Operation Result:

4. Inheritance
Subclasses have all the functions of the parent class

#InheritanceclassLang (object):def __init__(Self,name,score): Self.__name=name Self.score=scoredefLanginfo (self):Print '%s:%s'% (self.__name, Self.score)classC (Lang):PassclassPython (Lang):Passlang1=c ('C + +','9.0') Lang2=python ('Python','8.5') Lang1.langinfo () Lang2.langinfo ()

Operation Result:

How does a subclass have its own functionality? A method that defines the same type as the parent class in a subclass overrides the method of the parent class

classLang (object):def __init__(Self,name,score): Self.name=name Self.score=scoredefLanginfo (self):Print '%s:%s'%(Self.name,self.score)classC (Lang):PassclassPython (Lang):defLanginfo (self):Print "Hello%s!"%(self.name) lang1=c ('C + +','9.0') Lang2=python ('Python','8.5') Lang1.langinfo () Lang2.langinfo ()

Operation Result:

5. polymorphic
The subclass must be an instance of the parent class, and the parent class is not necessarily an instance of the child class

# polymorphicclassLang (object):def __init__(Self,name,score): Self.name=name Self.score=scoredefLanginfo (self):Print '%s:%s'%(Self.name,self.score)classC (Lang):PassclassPython (Lang):defLanginfo (self):Print "Hello%s!"%(self.name) lang1=c ('C + +','9.0') Lang2=python ('Python','8.5') Lang3=lang ('Lang','10.0')Printisinstance (lang1,c)Printisinstance (Lang1,lang)Printisinstance (lang3,c)PrintIsinstance (Lang3,lang)

Operation Result:

Because the subclass must be an instance of the parent class, the subclass instance can be declared as a parent class type

classLang (object):def __init__(Self,name,score): Self.name=name Self.score=scoredefLanginfo (self):Print '%s:%s'%(Self.name,self.score)classC (Lang):PassclassPython (Lang):defLanginfo (self):Print "Hello%s!"%(Self.name)defTest (lang): Lang.langinfo () lang1=c ('C + +','9.0') Lang2=python ('Python','8.5') test (lang1) test (lang2)

Operation Result:

When calling Langinfo, the method of the subclass instance is used, which is the meaning of polymorphism.

Python Object-oriented programming

Related Article

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.