Python Learning ($): Python object-oriented (1) Express

Source: Internet
Author: User

Transfer from http://www.cnblogs.com/BeginMan/p/3190776.html

First, the Python classic class and the new class

Classic class: If there is no direct or indirect subclass of an object, that is, if you do not specify a parent class, or if the subclass of the base class does not have a parent class, then the classic class is defined:

Class Classics:    'define a Classics class'    pass    

New class: Contrary to the classic class. Object is the "mother of all Classes", which is the base class, and if your class does not inherit any of the parent classes, object will be the default parent class:

Class Newclass (object):    'define a Classics Class'    pass    

Ii. instantiation of Python

The process of creating an instance is called instantiation, and the keyword new is generally used in other programming languages, but there is no such keyword in python. Instead, the instance is created in a way similar to a function call:

Class Newclass (object):    passinst = Newclass ()#<__main__.newclass object at 0x00b71650>     

The simplest usage of class Iii.

The simplest usage of a class is used only as a namespace, meaning that data is saved to a variable, and they are grouped by namespace using the period property identifier.

Class Newclass (object):    passbar = Newclass ()    ## Create Instance Properties Bar.y = 2print Bar.x+bar.y 

Note: bar.x, bar.y is an attribute of an instance object that is not a property of a class.

Iv. methods

method is the function of the class

The method is defined in the class and can only be called by the instance.

In a class, a method has a default argument of self, which represents the instance itself, which is passed silently by the interpreter to the method when the method is called with the instance.

Class Newclass (object):    def Method (self):        return self bar = Newclass ()#< __main__.newclass object at 0x00b71910>       

In other languages, self is generally referred to as this, and the general method uses this instance (self), but static and class methods are not required.

V. Create a class, method, instance object access

ClassAddperson (object):Def__init__ (SELF,NM,PH):#Defining the constructor Self.name = NM#Set name Self.phone = ph#Set up phonePrint‘Created instance for‘, Self.nameDefUpdatephone (self,newph): Self.phone =NewphPrint‘Updated Phone for‘, Self.phone josn = Addperson (‘Json‘,‘1565208444 ") # Create instance Object Josnben = Addperson ( ' ben ",  ' 15249638892") # Create an instance object Ben print  "instance property: [%s] [%s] # Access instance Properties Ben.updatephone (110 ")  Print Ben.phone               

Vi. Sub-categories

Create subclasses: Subclass by inheritance to improve your functionality without affecting the parent class (base class).

Such as: Inherit the above class

ClassAddinfo (Addperson):Def__init__ (Self,nm,ph,id,em):#Define your own constructor, Addperson.__init__ (self, NM, ph)#Inherits the parent class constructor (this is important) Self.empid =ID self.email =EmDefUpdateemail (SELF,NEWEM): Self.email =NewemPrint‘Updated e-mail address for:‘, Self.name Zhangsan = Addinfo (‘Zhangsan‘,‘123456789‘,‘01‘,‘[Email protected]‘)#Create Addinfo instance, output created instance for ZhangsanPrint Zhangsan#<__main__.addinfo Object at 0x00b71bd0>print 'Instance property:[%s],[%s],[%s],[%s]'%(zhangsan.name,zhangsan.phone,zhangsan.empid, Zhangsan.email)# output: Instance property:[zhangsan],[123456789],[01],[[email Protected]]zhangsan.updatephone (' + ') #Updated phone for 250zhangsan.updateemail ('[email protected]') # Updated e-mail address For:zhangsan              

Attention:

If necessary, each subclass is best to define its own constructor, otherwise the constructor of the base class will be called. However, if the subclass overrides the constructor of the base class, then the constructor of the base class is not automatically called and must be explicitly written out, as above: addperson.__init__

Python Learning ($): Python object-oriented (1) Express

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.