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()
    
  • 相关阅读:
    安装MySQL5.7.19 网上的文章参考 并做了部分修改
    从hadoop一路配置到spark
    java面试问题收集(2)
    JAVA的 IO NIO AIO笔记
    Shiro
    Spring注解使用注意点
    oracle RAC
    spark随笔
    Storm知识点笔记
    真机调试手机程序,电脑插上手机数据线虚拟机中的系统就死掉
  • 原文地址:https://www.cnblogs.com/allen2333/p/9297384.html
Copyright © 2011-2022 走看看