Python classes (Introduction, no content)

Source: Internet
Author: User

Reference:

https://www.cnblogs.com/studyDetail/p/6446180.html  (python的类)https://www.cnblogs.com/zunchang/p/7965685.html (面向过程和面向对象编程,推荐)http://xukaizijian.blog.163.com/blog/static/170433119201111894228877/ (python类的内置方法)

The definition of a class is somewhat like the following format:

class  ClassName([BaseClass]):    ClassAttribute=xx    def Method(self[,arg1,arg2...]):        self.instanceAttribute=xx    <statement>        ...    ...class是关键字ClassName:表示定义的类的名字BaseClass:这个是指要继承的基类的名字,如果为空,则默认继承object类ClassAttribute:定义在类的作用域则代表类的自有属性def Method(self[,arg1,arg2...]):这个是类里面的方法,只有类里面的方法才能对实例出来的数据进行操作self.instanceAttribute:实例属性,每个实例的值不一样,这是因为self关键字的缘故

Example 1: (Function of the Self field)

In [8]: Class C1 (): ...: info= ' from class ' ...: Def __init__ (self,myname): ...: Self.name=myname. .: Def printname (self): ...: Print self.name ...: in [9]: I1=c1 (' Tom ') in [ten]: i1.nameout[10] : ' Tom ' in [All]: I2=c1 (' Jerry ') in [[]: i2.nameout[12]: ' Jerry ' in [+]: i1.info,i2.infoout[13]: (' From-class ', ' from class ' __init__ (): This is the built-in method of the class, inherited from the superclass object, which is performed automatically when the class is instantiated (which is why the Python interpreter knows that it has a special meaning based on these tags and performs a special operation), which is why it starts with a two underscore. Without this function, we would have to manually execute the contents of the function. Self's role: it acts like a record instance name, such as I1=C1 (' Tom ') in this case, the Python interpreter automatically executes C1.__init_ (I1, ' Tom '), so this is replaced with the I1 instance name, And because Python forces self to be the first, we can guess that Python is passing the instance name based on its location rather than passing it on the Self keyword, which is also true. Proof: in [+]: Class C1 (): ...: info= ' from class ' ...: Def __init__ (s,myname): ...: S.name=myname ...: def PR Intname (s): ...: Print s.name ...: in []: i3=c1 (' hello ') in [Max]: i3.nameout[21]: ' Hello ' in []: I4=C1 (' word ') In [all]: i4.nameout[23]: ' word ' in [[]: i3.info,i4.infoout[24]: (' FRom class ', ' from class ') in [+]: c1.__init__ (I4, ' Jerry ') in []: i4.nameout[26]: ' Jerry ' is actually similar to a function, it's just an executable object, Just a few more related control mechanisms.

Example 2: (Inheritance and callbacks for classes)

In [28]: class c1():    ...:     info=‘from c1‘    ...:     def __init__(self,x):    ...:         self.name=x    ...:     def printInfo(self):    ...:         print self.name    ...:         In [30]: class c2(c1):    ...:     def printInfo(self):    ...:         print "hello,my name is : "    ...:         c1.printInfo(self)    ...:         In [31]: ins1=c2(‘jane‘)In [32]: ins1.printInfo()hello,my name is : janeIn [34]: c2.infoOut[34]: ‘from c1‘c2类继承了c1类的所有属性和方法,但是c2改写了c1的printInfo方法,在c2的方法中对c1的printInfo进行调用也就是所谓的"回调"

Example 3: (Effect of class attributes)

  in [all]: Class C1 (): ...: info= ' from C1 ' ...: def __init__ (self,x): ...: self.name=x ...: def printinfo (self): ...: Print self.name ...: in [K]: Ins1=c1 (' Tom ') in []: Ins2=c1 (' J Erry ') in [+]: ins3=c1 (' Boyka ') in [+]: ins1.info,ins2.info,ins3.infoout[15]: (' from-C1 ', ' from-C1 ', ' from-C1 ') in [+]: in s2.info= ' Change one ' in [+]: ins1.info,ins2.info,ins3.infoout[17]: (' from-C1 ', ' Change-one ', ' from-C1 ') in []: c1.info= ' Change all ins ' in [+]: ins1.info,ins2.info,ins3.infoout[19]: (' Change all ins ', ' Change One ', ' Change All ins ')  

From the above can be seen ins2 this instance changes its own Info property, does not affect the ins1 and INS3, and the C1 class info change, then changed ins1 and Ins3, this is because in the instantiation of INS1,INS2,INS3, They all point to the info pointer of the class, and the info pointer points to the ' from C1 ' data block. By default, when we visit Ins2.info, Python finds the class's info pointer through the INS2 info pointer, and then obtains the ' from C1 ' data through the class's info pointer. When Ins2.info is re-assigned, it creates a new data object ' Chang one ' and then points its info to ' change one ', so ins1,ins3 is not affected, and when C1.info is re-assigned, it is also a new ' change All ins ' object, and then the info pointer of the class points to ' Change all ins ', while ins1 and ins3 pointers still refer to the info pointer of the class, so ins1 and INS3 's info fetch values are changed ( this explanation is wrong, Because this ins1.info and Ins3.info ID should be the same, but my test is a change, here for the sake of understanding the memory for the time being so explained, to be identified)

Python classes (Introduction, no content)

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.