zoukankan      html  css  js  c++  java
  • 封装

    # class Foo:
    # __N=1
    # def __init__(self,x,y):
    # self.x=x
    # self.__y=y
    #
    # def __f1(self):
    # print('f1')
    #
    # def f2(self):
    # print(self.__N,self.__y)
    # print(Foo.__N)
    # print(Foo.__f1)
    '''
    AttributeError: type object 'Foo' has no attribute '__N'
    AttributeError: type object 'Foo' has no attribute '__f1'
    '''
    # print(Foo.__dict__)
    '''
    {'__module__': '__main__', '_Foo__N': 1,
    '__init__': <function Foo.__init__ at 0x000001C15BE34D38>,
    '_Foo__f1': <function Foo.__f1 at 0x000001C15BE34DC8>,
    '__dict__': <attribute '__dict__' of 'Foo' objects>,
    '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__doc__': None}
    '''
    # print(Foo._Foo__N)
    # print(Foo._Foo__f1)
    '''
    1
    <function Foo.__f1 at 0x00000292065D4DC8>
    '''
    # obj=Foo(1,2)
    # print(obj.__dict__)
    # print(obj._Foo__y)
    '''
    {'x': 1, '_Foo__y': 2}
    2
    '''
    # Foo.__M=2
    # print(Foo.__M)
    # print(Foo.__dict__)
    '''
    2
    {'__module__': '__main__', '_Foo__N': 1,
    '__init__': <function Foo.__init__ at 0x0000020E43E54D38>,
    '_Foo__f1': <function Foo.__f1 at 0x0000020E43E54DC8>,
    '__dict__': <attribute '__dict__' of 'Foo' objects>,
    '__weakref__': <attribute '__weakref__' of 'Foo' objects>,
    '__doc__': None, '__M': 2}
    '''
    '''
    __开头的属性只是一种语法意义上的变形,并不会真的限制外部的访问
    这种变形只在类定义阶段才会发生一次,类定义之后再新增的__开头的属性不会变形
    这种隐藏只对外不对内,因为类内部定义的属性在类定义阶段统一发生变形
    '''
    # obj=Foo(1,2)
    # print(obj.__dict__)
    # obj.__z=3
    # print(obj.__dict__)
    # print(obj.__z)
    '''
    {'x': 1, '_Foo__y': 2}
    {'x': 1, '_Foo__y': 2, '__z': 3}
    3
    '''
    # obj=Foo(2,2)
    # obj.f2()
    '''
    1 2
    '''
    # print(obj.__N)
    '''
    AttributeError: 'Foo' object has no attribute '__N'
    '''
    # class Foo:
    # def f1(self):
    # print('Foo f1')
    # class Bar(Foo):
    # def f1(self):
    # print('Bar.f1')
    #
    # b=Bar()
    # b.f1()
    '''
    Bar.f1
    '''
    '''
    class Foo:
    def __f1(self): # _Foo__f1
    print('Foo f1')
    class Bar(Foo):
    def __f1(self): # _Bar__f1
    print('Bar.f1')
    '''
    # class Foo:
    # def f1(self):
    # print('Foo f1')
    #
    # def f2(self):
    # print('Foo.f2')
    # self.f1()
    #
    # class Bar(Foo):
    # def f1(self):
    # print('Bar.f1')
    #
    # b=Bar()
    # b.f2()
    '''
    Foo.f2
    Bar.f1
    '''
    # class Foo:
    # def __f1(self):
    # print('Foo f1')
    #
    # def f2(self):
    # print('Foo.f2')
    # self.__f1()
    #
    # class Bar(Foo):
    # def __f1(self):
    # print('Bar.f1')
    #
    # b=Bar()
    # b.f2()
    '''
    Foo.f2
    Foo f1
    '''
    # class People:
    # def __init__(self,name,age):
    # self.__name=name
    # self.__age=age
    # def tell_info(self):
    # print('姓名:<%s>年龄:<%s>'%(self.__name,self.__age))
    # p=People('rambo',18)
    # p.tell_info()
    '''
    姓名:<rambo>年龄:<18>
    '''
    # class People:
    # def __init__(self,name,age):
    # self.set_info(name,age)
    # def tell_info(self):
    # print('姓名:<%s>年龄:<%s>'%(self.__name,self.__age))
    # def set_info(self,name,age):
    # self.__name=name
    # self.__age=age
    #
    # p=People('rambo',18)
    # p.set_info('Rambo',20)
    # p.tell_info()
    '''
    姓名:<Rambo>年龄:<20>
    '''
    # class People:
    # def __init__(self,name,age):
    # self.set_info(name,age)
    # def tell_info(self):
    # print('姓名:<%s>年龄:<%s>'%(self.__name,self.__age))
    # def set_info(self,name,age):
    # if type(name)is not str:
    # raise TypeError('name must be str')
    # if type(age)is not int:
    # raise TypeError('age must be int')
    # self.__name=name
    # self.__age=age
    #
    # p=People(1,18)
    # p.tell_info()
    '''
    TypeError: name must be str
    '''
    # class ATM:
    # def __card(self):
    # print('插卡')
    # def __auth(self):
    # print('用户认证')
    # def __input(self):
    # print('输入取款金额')
    # def __print_bill(self):
    # print('打印账单')
    # def __take_money(self):
    # print('取款')
    # def withdraw(self):
    # self.__card()
    # self.__auth()
    # self.__input()
    # self.__print_bill()
    # self.__take_money()
    # property
    # class People:
    # def __init__(self,name,age,height,weight):
    # self.name=name
    # self.age=age
    # self.height=height
    # self.weight=weight
    #
    # def bmi(self):
    # return self.weight / (self.height ** 2)
    #
    # rambo=People('rambo',18,1.75,65)
    # print(rambo.bmi())
    '''
    21.224489795918366
    '''
    # class People:
    # def __init__(self,name,age,height,weight):
    # self.name=name
    # self.age=age
    # self.height=height
    # self.weight=weight
    # @property
    # def bmi(self):
    # return self.weight / (self.height ** 2)

    # rambo=People('rambo',18,1.75,65)
    # print(rambo.bmi)
    '''
    21.224489795918366
    '''
    # rambo=People('rambo',18,1.75,65)
    # rambo.height=1.80
    # print(rambo.bmi)
    '''
    20.061728395061728
    '''
    # class People:
    # def __init__(self,name):
    # self.__name=name
    #
    # def get_name(self):
    # return self.__name
    #
    # rambo=People('rambo')
    # print(rambo.get_name())
    '''
    rambo
    '''
    # class People:
    # def __init__(self,name):
    # self.__name=name
    # @property
    # def get_name(self):
    # return self.__name
    #
    # rambo=People('rambo')
    # print(rambo.get_name)
    '''
    rambo
    '''
    # class People:
    # def __init__(self,name):
    # self.__name=name
    # @property
    # def name(self):
    # return self.__name
    #
    # rambo=People('rambo')
    # print(rambo.name)
    '''
    rambo
    '''
    # class People:
    # def __init__(self,name):
    # self.__name=name
    # @property
    # def name(self):
    # return self.__name
    # @name.setter
    # def name(self,obj):
    # self.__name=obj
    # rambo=People('rambo')
    # rambo.name='Rambo'
    # print(rambo.name)
    '''
    Rambo
    '''
    # class People:
    # def __init__(self,name):
    # self.__name=name
    # @property
    # def name(self):
    # return self.__name
    # @name.setter
    # def name(self,obj):
    # if type(obj)is not str:
    # raise TypeError('name must be str')
    # self.__name=obj
    # @name.deleter
    # def name(self):
    # del self.__name
    # rambo=People('rambo')
    # del rambo.name
    # print(rambo.name)
    '''
    AttributeError: 'People' object has no attribute '_People__name'
    '''
    # class People:
    # def __init__(self,name):
    # self.__name=name
    # @property
    # def name(self):
    # return self.__name
    # @name.setter
    # def name(self,obj):
    # if type(obj)is not str:
    # raise TypeError('name must be str')
    # self.__name=obj
    # @name.deleter
    # def name(self):
    # raise TypeError('不让删')
    # rambo=People('rambo')
    # del rambo.name
    # print(rambo.name)
    '''
    TypeError: 不让删
    '''
  • 相关阅读:
    网络编程 TCP
    网络编程之 osi七层协议
    面向对象之元类,单例
    面向对象之异常处理
    面向对象之多态
    面向对象之封装
    mysql 单表查询
    mysql 行(记录)的详细操作
    mysql 库表的操作
    数据库初识
  • 原文地址:https://www.cnblogs.com/0B0S/p/12092143.html
Copyright © 2011-2022 走看看