# 经典类写法# schoolMember.__init__(self, name, age, sex)# 新式类写法super(Teather, self).__init__(name, age, sex)
class schoolMember(object):'''学校成员分类'''member = 0def __init__(self, name, age, sex):self.name = nameself.age = ageself.sex = sexself.enroll() # 实例化时候 自动注册def enroll(self):'''注册'''print("just enrolled a new school member [%s]" % self.name)schoolMember.member += 1def tell(self):print("%s info".center(40, "-") % self.name)for i in self.__dict__: # 循环字典 t1.__dict__print("\t", i, self.__dict__[i])print("End".center(40, "-")+"\n")def __del__(self): # 析构函数print("开除了 [%s]" % self.name)schoolMember.member -= 1class Teather(schoolMember):'''教师类'''def __init__(self, name, age, sex, salary, course):# 经典类写法# schoolMember.__init__(self, name, age, sex)# 新式类写法super(Teather, self).__init__(name, age, sex)self.salary = salaryself.course = coursedef teaching(self):print("tearcher [%s] is teaching [%s]" % (self.name, self.course))class Student(schoolMember):'''学生类'''def __init__(self, name, age, sex, course, tuition):super(Student, self).__init__(name, age, sex)self.course = courseself.tuition = tuitionself.amount = 0def pay_tuition(self, amount):print("student [%s] has just paied [%s]" % (self.name, amount))self.amount += amountt1 = Teather("mike", 28, "F*M", 10000, "python") s1 = Student("jianlin", 21, "N/A","pythons15", 100000) s2 = Student("mayun", 12, "M", "pythons15", 210000)print(schoolMember.member)t1.tell() s1.tell() s2.tell()del s1 print(schoolMember.member) del s2 print(schoolMember.member) del t1 print(schoolMember.member)''' just enrolled a new school member [mike] just enrolled a new school member [jianlin] just enrolled a new school member [mayun] 3 ----------------mike info-----------------name mikeage 28sex F*Msalary 10000course python ------------------End-----------------------------------jianlin info-----------------name jianlinage 21sex N/Acourse pythons15tuition 100000amount 0 ------------------End-----------------------------------mayun info-----------------name mayunage 12sex Mcourse pythons15tuition 210000amount 0 ------------------End-------------------开除了 [jianlin] 2 开除了 [mayun] 1 开除了 [mike] 0 '''
class Person(object): # 新式类
super
class Person: # 经典类
Person.__init__()
现在主要写新式类