1.创建一个类
class Employee: empCount = 0 def __init__(self,name,salary): self.name = name self.salary = salary Employee.empCount +=1 def displayEmployee(self): print "Name:",self.name,",Salary:",self.salary#创建Employee类的一个对象emp1 = Employee("Zara",2000)emp2 = Employee("Manni",5000)emp1.displayEmployee()emp2.displayEmployee()print "Total Employee %d" % Employee.empCount
2.修改与删除属性
注:使用del a以后,它就像变量a从来没有存在过一样
class Employee: empCount = 0 def __init__(self,name,salary): self.name = name self.salary = salary Employee.empCount +=1 def displayEmployee(self): print "Name??",self.name,",Salary:",self.salaryemp1 = Employee("Zara",2000)emp2 = Employee("Manni",5000)#修改属性emp1.salary = "aa"emp1.displayEmployee() #删除属性#del emp2.salary;
3.Python对象销毁(垃圾回收)
Python使用了引用计数来追踪内存中的对象
一个内部跟踪对象,称为一个引用计数器
当对象被创建时,就创建了一个引用计数,当这个对象不再需要时,也就是说,这个对象的引用计数变为0时,它被垃圾回收。但是回收不是“立即”的,由解释器在适当的时机,将垃圾对象占用的内存空间回收
Python的垃圾收集器实际上是一个引用计数器和一个循环垃圾收集器
__del__是析构函数
class Point: def __init__(self,x=0,y=0): self.x = x self.y = y
def __del__(self): class_name = self.__class__.__name__ print class_name,"销毁"pt1 = Point()pt2 = pt1pt3 = pt2print id(pt1),id(pt2),id(pt3)del pt1del pt2del pt3
4.类的继承
class Parent: parentAttr = 100 def __init__(self): print "调用父类构造函数" def parentMethod(self): print "调用父类方法" def setAttr(self,attr): Parent.parentAttr = attr def getAttr(self): print "父类属性:",Parent.parentAttrclass Child(Parent): def __init__(self): print "调用子类构造方法" def childMethod(self): print "调用子类方法"child = Child()#调用子类构造方法child.childMethod()#调用子类方法child.parentMethod()#调用父类方法child.setAttr(200)child.getAttr()#父类属性: 200
例子2
#-*_coding:utf-8-*-class SchoolMember: def __init__(self,name,age): self.name = name self.age = age print 'Initialized SchoolMember:%s'%self.name def tell(self): print "'Name:%s' Age:%s"%(self.name,self.age)class Teacher(SchoolMember): def __init__(self,name,age,salary): SchoolMember.__init__(self,name,age) self.salary = salary#写了这句salary才会成为Teacher的属性 print 'Salary:%d'%self.salaryclass Student(SchoolMember): def __init__(self,name,age,marks): SchoolMember.__init__(self,name,age) self.marks = marks print 'Marks :%d'%self.markst = Teacher('Mrs.Lee',23,5000)s = Student('Jin',24,98)
5.类的多继承
class A: passclass B: passclass C(A,B): pass
6.调用子类方法
class Parent: def myMethod(self): print "调用父类方法" class Child(Parent): def myMethod(self): print "调用子类方法"child = Child()child.myMethod()#调用子类方法
7.运算符重载
class Vector: def __init__(self,a,b): self.a = a self.b = b def __add__(self,other): return Vector(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)print v1+v2#Vector(7,8)
8.声明类的私有成员
类的私有属性:__private_attrs:两个下划线开头,声明该属性为私有,不能在类的外部被使用,在类的内部使用self.__private_attrs
类的室友方法:__private_method:两个下划线开头,声明该方法为私有方法,不能在类的外部调用,在类的内部使用self__private_method
class JustCounter: __secreCount =0 publicCount =0 def count(self): self.__secreCount+=1 self.publicCount+=1 print self.__secreCountcounter = JustCounter()counter.count()counter.count()print counter.publicCountprint counter.__secreCount#报错,私有