继承顺序、多态性、封装作业
1.作业一:实现如图的继承关系,然后验证经典类与新式类在查找一个属性时的搜索顺序
class B:
def test(self):
print('from B')
pass
class C:
def test(self):
print('from C')
pass
class D(B,C):
def test(self):
print('from D')
pass
class E(B,C):
def test(self):
print('from E')
pass
class F(D,E):
def test(self):
print('from F')
pass
f1=F()
f1.test()
2.作业二:基于多态的概念来实现linux中一切皆问题的概念:文本文件,进程,磁盘都是文件,然后验证多态性
class Interface:
def read(self):
pass
def write(self):
pass
class Txt(Interface):
def read(self):
print('文本数据的读取方法')
def write(self):
print('文本数据的读取方法')
class Sata(Interface):
def read(self):
print('硬盘数据的读取方法')
def write(self):
print('硬盘数据的读取方法')
class Process(Interface):
def read(self):
print('进程数据的读取方法')
def write(self):
print('进程数据的读取方法')
def func(file):
file.read()
file.write()
t1=Txt()
s1=Sata()
p1=Process()
func(t1)
func(s1)
func(p1)
3.作业三:定义老师类,把老师的属性:薪资,隐藏起来,然后针对该属性开放访问接口
苑昊老师有多种癖好,把这种癖好隐藏起来,然后对外提供访问接口
而且以后还会苑昊老师培养很多其他的癖好,对外开放修改接口,可以新增癖好,并且需要保证新增的癖好都是字符串类型,否则无法增加成功。
class Teacher:
def __init__(self,name,age,sex,pay,foible=""):
self.name = name
self.age = age
self.sex = sex
self.__pay = pay
self.__foible = foible
def get_pay(self):
return 'your pay is %s' % self.__pay
def set_foible(self,foible):
self.__foible=foible
return 'your foible is %s'%self.__foible
t1=Teacher("苑昊",'18','female','500','丝袜')
e1=Teacher("egon",'18','female','800','黄瓜')
print(t1._Teacher__pay)
print(t1._Teacher__foible)
print(e1.set_foible('猪蹄'))
print(e1.set_foible('开车'))

浙公网安备 33010602011771号