python 面向对象 新式类和经典类

时间:2022-06-05 14:33:27
 # 经典类写法
# schoolMember.__init__(self, name, age, sex) # 新式类写法
super(Teather, self).__init__(name, age, sex)
class schoolMember(object):
'''学校成员分类'''
member = 0 def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex self.enroll() # 实例化时候 自动注册 def enroll(self):
'''注册'''
print("just enrolled a new school member [%s]" % self.name)
schoolMember.member += 1 def 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 -= 1 class 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 = salary
self.course = course def 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 = course
self.tuition = tuition
self.amount = 0 def pay_tuition(self, amount):
print("student [%s] has just paied [%s]" % (self.name, amount))
self.amount += amount t1 = 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 mike
age 28
sex F*M
salary 10000
course python
------------------End------------------- ----------------jianlin info-----------------
name jianlin
age 21
sex N/A
course pythons15
tuition 100000
amount 0
------------------End------------------- ----------------mayun info-----------------
name mayun
age 12
sex M
course pythons15
tuition 210000
amount 0
------------------End------------------- 开除了 [jianlin]
2
开除了 [mayun]
1
开除了 [mike]
0
'''
class Person(object):  # 新式类
super class Person: # 经典类
Person.__init__() 现在主要写新式类 python 面向对象 新式类和经典类

python 面向对象 新式类和经典类