[Turn]python Self, class, construct, and destructor methods to learn simple

Source: Internet
Author: User

Self can be understood as
Similar to the this pointer in C + +, the object itself means that when the method is called with an object, the object is passed as the first argument to self

There are properties, methods in the Python class, where properties can have private and public points, and methods can qualify and protect private properties, similar to the functionality of properties in C #.

Method also has private and public, __ method front two underline

Class, a collection of entities with similar internal and motion laws (collectively, abstractly)
A generic term that has the same attributes and behavior, the class is abstract, the concrete existence of the class is found, using this concrete existence--object
Class is the template class for an object: Class name, property, method

classDog:color='Red' #without underscores, objects can be accessed outside of the class: publicage=10__height=10#private attribute, this place is preceded by an underscore, which means that it cannot be used outside    defSetName (self,newname): Self.name=newName self.__weight=20defprintheight (self):Print('The height is%d'%self.__height)        Print('The weight is%d'%self.__weight) Dog=Dog () dog.setname ('Bule')#This method is called before the private __weight can be accessedPrint(Dog.color) dog.printheight ()#property is used to hold the data, and the private property represents the encapsulation#the self in the method must be the first parameter used to receive the object using the

Self, you can replace the object with self in the method

classPerson ():#constructs a method that is called by default while creating an object    def __init__(self): Self.country='Chinese'    def __del__(self):Print('destructor Method')#when an object is deleted, the destructor method is called by default    __name='Lily'    __age=12"""docstring for ClassName"""    def __getname(self):#properties can be qualified in this method, similar to the property features in C #        returnSelf.__name    def __getage(self):#qualification and protection of attributes        ifSelf.__age>18:              returnSelf.__age        Else:              return18defgetnameage (self): name1=self.__getname() Age1=self.__getage()        Print('name is%s,age is%d'%(name1,age1)) person=Person () person.getnameage ()#print (P.getage (), P.getname ()) #两个方法是私有的, cannot be called outside the classdelPerson#call a destructor method

Method can return tuple def test:return (100,200,200) a,b,c=test ()

classTeacher:age=30def __init__(self):Print('how to construct a parent class')    defShowName (self,name): Self.name=namePrint('Name is%s'%self.name)defshowage (self,age): Self.age= AgePrint('Age is %d'%self.age)classStudent (Teacher):#The pass# subclass inherits the parent class, and the parent class is a subclass of objects that contain, write only the pass    def __init__(self): Teacher.__init__(self)#depending on the order of the two rows, the parent class or subclass method is determined first        Print('subclasses override the methods of the parent class, and the child class is constructed by')            defSpecial (self):Print('Student is a child')classDoctor ():Pass#class PEL (teacher,student): #多继承, error cannot create a consistent method resolution, inherit too much#Passclasspe2 (teacher,doctor):PassTeacher=Teacher () Student=Student () teacher.showage (30) Teacher.showname ('Mr Li')#the method name of the child class is the same as the method name of the parent class, overriding. The object of the subclass calls the child class's#polymorphic concepts are used in strongly typed languages such as Java and C #, while Python advocates ' duck type '

[Turn]python Self, class, construct, and destructor methods to learn simple

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.