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