继承和封装

#子类调用父类
# class People:
#     def __init__(self,name,age,sex):
#         self.name=name
#         self.age=age
#         self.sex=sex
#     def foo(self):
#         print('from parent')
# class Teacher(People):
#     def __init__(self,name,age,sex,salary,level):
#         # People.__init__(self,name,age,sex) #指名道姓地调用People类的__init__函数
#
#         #在python3中
#         super().__init__(name,age,sex) #调用父类的__init__的功能,实际上用的是绑定方法
#
#         #在python2中
#         # super(Teacher,self).__init__(name,age,sex)
#
#
#         self.salary=salary
#         self.level=level
#     def foo(self):
#         super().foo()
#         print('from child')
#
#
# t=Teacher('egon',18,'male',3000,10)
# # print(t.name,t.age,t.sex,t.salary,t.level)
# t.foo()






#继承
#coding:utf-8
# class A(object):
#     def test(self):
#         print('from A')
#     pass
# class B(A):
#     # def test(self):
#     #     print('from B')
#     pass
# class C(A):
#     # def test(self):
#     #     print('from C')
#     pass
# class D(A):
#     # def test(self):
#     #     print('from D')
#     pass
# class E(B):
#     # def test(self):
#     #     print('from E')
#     pass
# class F(C):
#     # def test(self):
#     #     print('from F')
#     pass
# class G(D):
#     # def test(self):
#     #     print('from G')
#     pass
# class H(E,F,G):
#     # def test(self):
#     #     print('from H')
#     pass
# h=H()
# # h.test=1
# # print h.__dict__
# #新式类的在这中继承结构下,属性的查找关系
# # h.test()
#
# # H->E->B->F->C-G-D-A 广度优先
# #mro
# print(H.mro())
#
# #coding:utf-8
# class A:
#     # def test(self):
#     #     print('from A')
#     pass
# class B(A):
#     # def test(self):
#     #     print('from B')
#     pass
# class C(A):
#     # def test(self):
#     #     print('from C')
#     pass
# class D(A):
#     # def test(self):
#     #     print('from D')
#     pass
# class E(B):
#     # def test(self):
#     #     print('from E')
#     pass
# class F(C):
#     # def test(self):
#     #     print('from F')
#     pass
# class G(D):
#     # def test(self):
#     #     print('from G')
#     pass
# class H(E,F,G):
#     # def test(self):
#     #     print('from H')
#     pass
# h=H()
# #经典类的在这中继承结构下,属性的查找关系
# h.test()
# H-E-B-A-F-C-G-D 深度优先

#封装
# class People:
#     def __init__(self,name,age,sex,height,weight,permission=False):
#         self.__name=name
#         self.__age=age
#         self.__sex=sex
#         self.__height=height
#         self.__weight=weight
#         self.permission=permission
#     def tell_info(self):
#         print('''
#         ---------%s info
#         name:%s
#         age:%s
#         sex:%s
#         height:%s
#         weight:%s
#         ''' %(self.__name,
#               self.__name,
#               self.__age,
#               self.__sex,
#               self.__height,
#               self.__weight))
#     @property
#     def name(self):#访问姓名
#         return self.__name
#     @name.setter#,修改姓名并加上类型检查
#     def name(self,val):
#         if not isinstance(val,str):
#             raise  TypeError('must be str')
#         self.__name=val
#     @name.deleter#删除姓名
#     def name(self):
#         if not self.permission:
#             raise PermissionError('不让删')
#         del self.__name
#     @property
#     def bmi(self):
#         res = self.__weight / (self.__height ** 2)
#         return res
# p=People('z',18,'male',1.75,60)
# p.tell_info()#访问人的详细信息
# print(p.name)#访问姓名
# p.name='x'#修改姓名
# p.permission=True
# del p.name#删除姓名
# print(p.bmi)#访问人的bmi指数

 

设置自己的逻辑是封装真正的意义

原文地址:https://www.cnblogs.com/z-x-y/p/7124258.html