多态与多态性 简单的“__”封装

基于多态的概念来实现linux中一切皆问题的概念:文本文件,进程,磁盘都是文件,然后验证多态性
class
File: def read(self): raise AttributeError("此功能必须实现") def write(self): raise AttributeError("此功能必须实现") class Txt(File): def read(self): print("Txt start to read") def write(self): print("Txt start to write") class Progress(File): def read(self): print("Progress start to read") def write(self): print("Progress start to write") class Sata(File): def read(self): print("Sata start to read") def write(self): print("Sata start to write") s=Sata() def fuc_read(obj): obj.read() def fuc_write(obj): obj.write() fuc_read(s)





定义老师类,把老师的属性:薪资,隐藏起来,然后针对该属性开放访问接口
苑昊老师有多种癖好,把这种癖好隐藏起来,然后对外提供访问接口
而且以后还会苑昊老师培养很多其他的癖好,对外开放修改接口,可以新增癖好,并且需要保证新增的癖好都是字符串类型,否则无法增加成功。


class
Teacher: def __init__(self,name,age,salary,hobbys,*args): self.name=name self.age=age self.__salary=salary self.__hobby=[hobbys,*args] def search_salary(self): print("%s salary is %s"%(self.name,self.__salary)) def search_hobby(self): print("%s hobby is %s"%(self.name,self.__hobby)) def hobby_append(self,hobby_app): if type(hobby_app)==str: self.__hobby.append(hobby_app) print("%s append success"%hobby_app) else: print("hobby_app type is error") t=Teacher("苑浩",22,50000,"Fuck","Tian") t.hobby_append("Cha") t.hobby_append(1) t.search_hobby() t.search_salary()

 

 

 

posted @ 2017-04-20 16:32  人到中年万事休  阅读(177)  评论(0编辑  收藏  举报