Python Object-oriented

Source: Internet
Author: User

1. Create a class

classEmployee:empcount=0def __init__(self,name,salary): Self.name=name Self.salary=Salary Employee.empcount+=1defDisplayemployee (self):Print "Name:", Self.name,", Salary:", Self.salary#create an object for the employee classEMP1 = Employee ("Zara", 2000) EMP2= Employee ("Manni", 5000) Emp1.displayemployee () Emp2.displayemployee ()Print "Total Employee%d"% Employee.empcount

2. Modifying and Deleting properties

classEmployee:empcount=0def __init__(self,name,salary): Self.name=name Self.salary=Salary Employee.empcount+=1defDisplayemployee (self):Print "Name??", Self.name,", Salary:", Self.salaryemp1= Employee ("Zara", 2000) EMP2= Employee ("Manni", 5000)#Modifying PropertiesEmp1.salary ="AA"Emp1.displayemployee ()#Delete Property#del emp2.salary;

3.Python Object Destruction (Garbage collection)

Python uses reference counts to track objects in memory

An internal tracking object, called a reference counter

When the object is created, a reference count is created, and when the object is no longer needed, that is, the reference count of the object becomes 0 o'clock, and it is garbage collected. However, recycling is not "immediate", and the interpreter uses the garbage object to reclaim the memory space at the appropriate time.

Python's garbage collector is actually a reference counter and a cyclic garbage collector

__del__ is a destructor

 class   point:  def  __init__  (Self,x=0,y=0): self.x  = x self.y  = y  
    def __del__ (self):         = self. __class__. __name__        print class_name," destroy "== = Pt2  print  ID (PT1), id (PT2), id (PT3)del  pt1del  pt2  del PT3

4. Inheritance of classes

classparent:parentattr= 100def __init__(self):Print "calling the parent class constructor"    defParentmethod (self):Print "calling the parent class method"    defsetAttr (self,attr): Parent.parentattr=attrdefgetAttr (self):Print "Parent class Properties:", Parent.parentattrclassChild (Parent):def __init__(self):Print "calling subclass construction methods"    defChildmethod (self):Print "Calling Subclass Methods" Child= Child ()#calling subclass construction methodsChild.childmethod ()#Calling Subclass MethodsChild.parentmethod ()#calling the parent class methodCHILD.SETATTR (200) child.getattr ()#Parent Class Property:

5. Multiple Inheritance of classes

class A:     Pass class B:     Pass class C (A, b    ): Pass

6. Calling Subclass methods

class Parent:     def MyMethod (self):         Print " calling the parent class method "    class Child (Parent):     def MyMethod (self):         Print " Calling Subclass Methods "  = Child () Child.mymethod ()# Call subclass Method

7. Operator overloading

classVector:def __init__(self,a,b): SELF.A=a self.b=bdef __add__(self,other):returnVector (SELF.A + other.a,self.b +other.b)def __str__(self):return "Vector (%d,%d)"%(self.a,self.b) V1= Vector (2,10) V2= Vector (5,-2)PrintV1+v2#Vector (7,8)

8. Declaring a private member of a class

The private property of the class: __private_attrs: Two underscores, declares that the property is private, cannot be used outside of the class, and is used inside the class Self.__private_attrs

Class of roommate Method: __private_method: Two underscore begins, declares that the method is a private method, cannot be called outside of the class, uses Self__private_method inside the class

class Justcounter:     __secrecount =0    =0    def  count (self): self        . __secrecount+=1        self.publiccount+=1        print self.  __secrecount= justcounter () Counter.count () Counter.count ()print  Counter.publiccountPrint counter. __secrecount # Error, Private

Python Object-oriented

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.