zoukankan      html  css  js  c++  java
  • 多态与多态性 简单的“__”封装

    基于多态的概念来实现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()
    
    
    
     
  • 相关阅读:
    wmware虚拟系统光盘的问题
    ORM框架SQLAlchemy
    python关于二分查找
    Python的各种推导式合集
    远程连接腾讯云服务器MySQL数据库
    Django配置404页面
    使用Python将Excel中的数据导入到MySQL
    MySQLl导入导出SQL文件
    数据结构(十三)排序
    数据结构(十二)散列表
  • 原文地址:https://www.cnblogs.com/chedanlangren/p/6739478.html
Copyright © 2011-2022 走看看