zoukankan      html  css  js  c++  java
  • 计算机基础

    封装这个概念贯穿码农工作的日常, 意义:

    1. 隐藏数据,对外提供操作数据的接口,并可以对操作数据进行严格控制。

    class Teacher:
    
        def __init__(self, name, age):
            self.__name = name
            self.__age = age
    
        def tell_info(self):
            print('姓名:{name}, 年龄:{age}'.format(name=self.__name, age=self.__age))
    
        def set_info(self, name, age):
    
            # 对输入进行检测,对操作数据属性进行严格控制
            if not isinstance(name, str):
                raise TypeError('name must be string')
            if not isinstance(age, int):
                raise TypeError('age must be int')
            # 过了检测后,赋值
            self.__name = name
            self.__age = age
    
    betty = Teacher('betty', 25)
    betty.tell_info()
    #betty.set_info(1, 1)
    #betty.set_info('x', 'x')
    betty.set_info('allen', 23)
    betty.tell_info()
    

    2. 隔离复杂度

    # 取款是功能,由插卡、密码认证、输入金额、打印账单、取钱组成
    # 使用者只知道这个接口完成取款操作,不需要知道细节
    # 这样做隔离了复杂度,提高了安全性
    
    class ATM:
    
        def __insert_card(self):
            print('insert the freaking card')
    
        def __auth(self):
            print('password auth')
    
        def __input(self):
            print('input money')
    
        def __print_bill(self):
            print('print bill')
    
        def __take_money(self):
            print('take money')
    
        def withdraw(self):
            self.__insert_card()
            self.__auth()
            self.__input()
            self.__print_bill()
            self.__take_money()
    
    Automatic_Teller_Machine = ATM()
    Automatic_Teller_Machine.withdraw()
    
  • 相关阅读:
    转 GFlags 使用详解
    printf 格式输出
    XCODE unknown type name __declspec 错误的解决方法
    Boost提示'cl' 不是内部或外部命令,也不是可运行的程序 或批处理文件
    DOLServer
    游戏AI的开发框架组件 behaviac
    mongodb 数据导入和导出
    Makefile经典教程
    g++ 编译动态链接库和静态链接库
    excel 公式
  • 原文地址:https://www.cnblogs.com/allen2333/p/9297384.html
Copyright © 2011-2022 走看看