The abstraction of the basic Python tutorial

Source: Internet
Author: User

I knew Python early, but didn't insist on learning. Recent whim, but can make up for this regret.

Object several important concepts:

Polymorphic: The same operation can be used for objects of different classes;

Encapsulation: Working details of hidden objects in the external world;

Inheritance: A specialized class object is built on the basis of a common class.

Examples of inheritance:

Class Addrbookentry (object):
' Address Book entry class '
def __init__ (self, NM, ph):
Self.name = NM
Self.phone = ph
print ' created instance for: ', self.name

def updatephone (self, newph):
Self.phone = newph
print ' Updated phone# for: ', self.name

Sub-class:
Class Empladdrbookentry (Addrbookentry):
' Employee Address Book entry class '
def __init__ (self, NM, ph, id, EM):
Addrbookentry.__init__ (self, NM, ph)
Self.empid = ID
Self.email = em

def updateemail (self, NEWEM):
Self.email = Newem
print ' Updated e-mail address for: ', Self.email

Polymorphic examples

Polymorphism is a property that appears after the instantiation of a class, meaning that even if you do not know what type of object is being referenced, you can manipulate it, and it will behave differently depending on the type of object.

In the inheritance process, it is more recessive that the function interface of the parent class will be overwritten when the subclass redefine the functional interface of the parent class.

Class Human (object):
def run (self):
print ' Human is running ... '


Class Man (Human):
Pass

Class Woman (Human):
Pass

John = Man ()
Jude = Woman ()

John.run ()
Jude.run ()

Class Man1 (Human):
def run (self):
print ' Man is running ... '
Pass

Class Woman1 (Human):
def run (self):
print ' Woman is running ... '
Pass

John = Man1 ()
Jude = Woman1 ()

John.run ()
Jude.run ()

The abstraction of the basic Python tutorial

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.