zoukankan      html  css  js  c++  java
  • python入门第二十四天----成员修饰符 类的特殊成员

    1 #成员修饰符  修饰符可以规定内部的字段、属性、方法等 是共有的成员,私有的成员
    2 class Foo:
    3     def __init__(self,name,age):
    4         self.name=name
    5         self.age=age  #可以在外部直接访问
    6 
    7 obj=Foo('Jack',22)
    8 print(obj.name)
    9 print(obj.age)
    共有字段

    1 #成员修饰符  修饰符可以规定内部的字段、属性、方法等 是共有的成员,私有的成员
    2 class Foo:
    3     def __init__(self,name,age):
    4         self.name=name
    5         #self.age=age  #可以在外部直接访问
    6         self.__age=age #添加 __ 两个下划线,就变成私有字段,外部不能访问
    7 obj=Foo('Jack',22)
    8 print(obj.name)
    9 print(obj.age)

    运行结果:

    "D:Program Files (x86)python36python.exe" F:/python从入门到放弃/7.24/面向对象.py
    Traceback (most recent call last):
    Jack
      File "F:/python从入门到放弃/7.24/面向对象.py", line 16, in <module>
        print(obj.age)
    AttributeError: 'Foo' object has no attribute 'age'
    
    Process finished with exit code 1

    私有字段外部不能直接访问。可以通过内部的方法,间接访问

     1 #成员修饰符  修饰符可以规定内部的字段、属性、方法等 是共有的成员,私有的成员
     2 class Foo:
     3     def __init__(self,name,age):
     4         self.name=name
     5         #self.age=age  #可以在外部直接访问
     6         self.__age=age #添加 __ 两个下划线,就变成私有字段,外部不能访问
     7     def get_age(self):
     8         return self.__age
     9 obj=Foo('Jack',22)
    10 print(obj.name)
    11 print(obj.get_age())

    运行结果:

    "D:Program Files (x86)python36python.exe" F:/python从入门到放弃/7.24/面向对象.py
    Jack
    22
    
    Process finished with exit code 0
     1 #成员修饰符  修饰符可以规定内部的字段、属性、方法等 是共有的成员,私有的成员
     2 class Foo:
     3     gender='' #对于静态字段
     4     def __init__(self,name,age):
     5         self.name=name
     6         #self.age=age  #对于普通字段可以在外部直接访问
     7         self.__age=age #添加 __ 两个下划线,就变成私有字段,外部不能访问
     8     def get_age(self):
     9         return self.__age
    10 obj=Foo('Jack',22)
    11 # print(obj.name)
    12 # print(obj.get_age())
    13 print(obj.gender)
    静态字段
     1 class Foo:
     2     #gender='男' #对于静态字段
     3     __gender = ''
     4     def __init__(self,name,age):
     5         self.name=name
     6         #self.age=age  #对于普通字段可以在外部直接访问
     7         self.__age=age #添加 __ 两个下划线,就变成私有字段,外部不能访问
     8     def get_age(self):
     9         return self.__age
    10     def get_gender(self):
    11         return Foo.__gender
    12 obj=Foo('Jack',22)
    13 # print(obj.name)
    14 # print(obj.get_age())
    15 print(obj.get_gender())
    私有静态字段
     1 class Foo:
     2     __gender=''
     3     def __init__(self):
     4         pass
     5 
     6     # def get_gender(self):
     7     #     return Foo.__gender
     8 
     9     @staticmethod
    10     def get_gender():
    11         return Foo.__gender
    12 # obj=Foo()
    13 # print(obj.get_gender())
    14 print(Foo.get_gender())
    通过静态方法,访问私有字段

     1 #案例 数据库账户
     2 class Database:
     3     __root="root"
     4     __pwd='abc123'
     5     __port='3306'
     6     __dbname='ssoa'
     7     # def __init__(self,pwd):
     8     #     pass
     9 ###############只读模式########################
    10     def get_root(self): #只能访问,不能修改  可读
    11         return self.__root
    12     def get_port(self):
    13         return self.__port
    14     
    15     ###########读写模式 #################
    16     def get_pwd(self):  #获取密码
    17         return self.__pwd
    18     def set_pwd(self,pwd):  #修改密码
    19         self.__pwd = pwd
    20 
    21 db=  Database()
    22 print(db.get_pwd()) #调用密码
    23 db.set_pwd('456')   #修改密码
    24 print(db.get_pwd())#调用密码
    读写 只读
     1 class ClassFather:
     2     def __init__(self):
     3         self.__age=23
     4         self.score=90
     5 
     6 class ClassSon(ClassFather):
     7     def __init__(self,name):
     8         self.name=name
     9         self.__gender=''
    10         super().__init__()
    11 
    12     def show(self):
    13         print(self.name)
    14         print(self.__gender)
    15         print(self.score)
    16         #print(self.__age)#私有字段只能在类的内部使用,不能被继承
    17 
    18 s=ClassSon('李逵')
    19 s.show()
    私有字段只能在类的内部使用,不能被继承



    类的特殊成员

     __init__/__call__

     1 class Foo:
     2     def __init__(self): #对象后面加上() 自动执行 init 方法
     3         print('init')
     4 
     5 
     6     def __call__(self, *args, **kwargs):  ##对象后面加() 自动执行 call 方法
     7         print('call')
     8 
     9 
    10 obj=Foo()
    11 obj()
    ##相当于 Foo()()

    __int__/__str__

     1 class Foo:
     2     def __init__(self):
     3         pass
     4     def __int__(self):
     5         return 111
     6     def __str__(self):
     7         return 'string'
     8 obj=Foo()
     9 
    10 print(obj,'
    ',type(obj))
    11 
    12 #int ,对象,自动执行对象的__int__ 方法,并将返回值赋值给int对象
    13 r=int(obj)
    14 print(r)
    15 #str ,对象,自动执行对象的__str__ 方法,并将返回值赋值给str对象
    16 t=str(obj)
    17 print(t)

    运行结果:

    "D:Program Files (x86)python36python.exe" F:/python从入门到放弃/7.24/面向对象.py
    string 
     <class '__main__.Foo'>
    111
    string
    
    Process finished with exit code 0

    在实际使用中,使用__str__() 方法的频率更大

    例如:

    1 class Foo:
    2     def __init__(self,name,age):
    3         self.name=name
    4         self.age=age
    5 
    6 obj=Foo('Jach',12)
    7 print(obj)

    运行结果:

    "D:Program Files (x86)python36python.exe" F:/python从入门到放弃/7.24/面向对象.py
    <__main__.Foo object at 0x000000000222D1D0>
    
    Process finished with exit code 0

     1 class Foo:
     2     def __init__(self,name,age):
     3         self.name=name
     4         self.age=age
     5 
     6     def __str__(self): #用于打印
     7         return '%s----%s'%(self.name,self.age)
     8 
     9 obj=Foo('Jach',12)
    10 #如果直接调用对象打印出来,会默认直接调用 __str__ 方法
    11 #内部先把'print(obj)'默认转换成>>>'print(str(obj))' 获取其返回值 
    12 print(obj)

    运行结果:

    "D:Program Files (x86)python36python.exe" F:/python从入门到放弃/7.24/面向对象.py
    Jach----12
    
    Process finished with exit code 0

    __add__

     1 class Foo:
     2     def __init__(self,name,age):
     3         self.name=name
     4         self.age=age
     5 
     6     def __add__(self, other):
     7         #self == obj('亚瑟',12)
     8         #other==obj('后羿',23)
     9         # return self.age+other.age
    10         return Foo(self.name,other.age)
    11 
    12 obj=Foo('亚瑟',12)
    13 obj2=Foo('后羿',23)
    14 
    15 r=obj+obj2
    16 #两个相加时,会自动执行第一个对象的 __add__ 方法,并且把第二个参数当做参数传递进去
    17 print(r,type(r))
    18 print(r.name)
    19 print(r.age)

    运行结果:

    "D:Program Files (x86)python36python.exe" F:/python从入门到放弃/7.24/面向对象.py
    <__main__.Foo object at 0x00000000024BD4E0> <class '__main__.Foo'>
    亚瑟
    23
    
    Process finished with exit code 0

    构造方法 对象被创造的时候自动触发  __init__

    析构方法 对象被销毁的时候自动触发  __del__ 

     1 class Foo:
     2     def __init__(self,name,age):
     3         self.name=name
     4         self.age=age
     5 
     6     def __add__(self, other):
     7         #self == obj('亚瑟',12)
     8         #other==obj('后羿',23)
     9         # return self.age+other.age
    10         return Foo(self.name,other.age)
    11     def __del__(self):
    12         print('析构方法,对象销毁时,自动执行')
    13 
    14 obj=Foo('亚瑟',12)
    15 obj2=Foo('后羿',23)
    16 
    17 r=obj+obj2
    18 #两个相加时,会自动执行第一个对象的 __add__ 方法,并且把第二个参数当做参数传递进去
    19 print(r,type(r))
    20 print(r.name)
    21 print(r.age)

    运行结果:

    "D:Program Files (x86)python36python.exe" F:/python从入门到放弃/7.24/面向对象.py
    <__main__.Foo object at 0x0000000001EAD470> <class '__main__.Foo'>
    亚瑟
    23
    析构方法,对象销毁时,自动执行
    析构方法,对象销毁时,自动执行
    析构方法,对象销毁时,自动执行
    
    Process finished with exit code 0

    __dict__  #将对象中分装的所有成员,通过字典的形式,返回

     1 class Foo:
     2     def __init__(self,name,age):
     3         self.name=name
     4         self.age=age
     5 
     6     def __add__(self, other):
     7         #self == obj('亚瑟',12)
     8         #other==obj('后羿',23)
     9         # return self.age+other.age
    10         return Foo(self.name,other.age)
    11     def __del__(self):
    12         print('析构方法,对象销毁时,自动执行')
    13 
    14 
    15 obj=Foo('亚瑟',12)
    16 d=obj.__dict__
    17 print(d)

    运行结果:

    "D:Program Files (x86)python36python.exe" F:/python从入门到放弃/7.24/面向对象.py
    {'name': '亚瑟', 'age': 12}
    析构方法,对象销毁时,自动执行
    
    Process finished with exit code 0

    __doc__ 文档说明 表示类的描述信息

    class Foo:
        """ 描述类信息,这是用于看片的神奇 """
    
        def func(self):
            pass
    
    print Foo.__doc__
    #输出:类的描述信息
    __doc__ 1
     1 class Foo:
     2     '''
     3     当前的简要说明,参数设置等信息
     4     '''
     5     def __init__(self,name,age):
     6         self.name=name
     7         self.age=age
     8 
     9     def __add__(self, other):
    10         #self == obj('亚瑟',12)
    11         #other==obj('后羿',23)
    12         # return self.age+other.age
    13         return Foo(self.name,other.age)
    14     def __del__(self):
    15         print('析构方法,对象销毁时,自动执行')
    16 
    17 # obj=Foo('亚瑟',12)
    18 # d=obj.__dict__
    19 # print(d)
    20 print(Foo.__dict__)
    __doc__
    1 "D:Program Files (x86)python36python.exe" F:/python从入门到放弃/7.24/面向对象.py
    2 {'__module__': '__main__', '__doc__': '
        当前的简要说明,参数设置等信息
        ', '__init__': <function Foo.__init__ at 0x00000000021FD2F0>, '__add__': <function Foo.__add__ at 0x00000000021FD378>, '__del__': <function Foo.__del__ at 0x00000000021FD400>, '__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>}
    3 
    4 Process finished with exit code 0
    运行结果

    __getitem__/__setitem__/__delitem__

     1 class Foo:
     2     def __init__(self,name,age):
     3         self.name=name
     4         self.age=age
     5     def __getitem__(self, item):
     6         return item+10
     7     def __setitem__(self, key, value):
     8         print(key,value)
     9     def __delitem__(self, key):
    10         print(key)
    11 
    12 obj=Foo('亚瑟',12)
    13 r=obj[8]#在对象后面后加上[] 以索引的方式访问,会自动执行  __getitem__方法
    14 print(r)
    15 
    16 obj[50]='得得得得得得'#通过索引赋值的访问,会自动执行  __setitem__ 方法
    17 
    18 del obj[50]

    运行结果:

    "D:Program Files (x86)python36python.exe" F:/python从入门到放弃/7.24/面向对象.py
    18
    50 得得得得得得
    50
    
    Process finished with exit code 0

    slice 切片类型的内部方法:

    class slice(object):
        
       #............ 
       
        start = property(lambda self: 0)
        """:type: int"""
    
        step = property(lambda self: 0)
        """:type: int"""
    
        stop = property(lambda self: 0)
        """:type: int"""
    
    
        __hash__ = None

    __getitem__切片或是索引

     1 class Foo:
     2     def __init__(self,name,age):
     3         self.name=name
     4         self.age=age
     5     def __getitem__(self, item):
     6         print(item,type(item))
     7         # return item+10
     8         if type(item)==slice:
     9             print('切片处理')
    10             print(item.start)
    11             print(item.step)
    12             print(item.stop)
    13         else:
    14             
    15             print("索引处理")
    16     def __setitem__(self, key, value):
    17         print(key,value)
    18     def __delitem__(self, key):
    19         print(key)
    20 
    21 obj=Foo('亚瑟',12)
    22 # r=obj[8]#在对象后面后加上[] 以索引的方式访问,会自动执行  __getitem__方法
    23 # print(r)
    24 #
    25 # obj[50]='得得得得得得'#通过索引赋值的访问,会自动执行  __setitem__ 方法
    26 #
    27 # del obj[50]
    28 obj[123]
    29 obj[1:4:2]

    运行结果:

    "D:Program Files (x86)python36python.exe" F:/python从入门到放弃/7.24/面向对象.py
    123 <class 'int'>
    索引处理
    slice(1, 4, 2) <class 'slice'>
    切片处理
    1
    2
    4

    __iter__

    #如果类中有__iter__方法,对象=》可迭代对象
    #对象.__iter__()的返回值:迭代器
    # for 循环,迭代器,next
    # for循环,可迭代对象,对象.__iter__(),迭代器,next
    #1、执行对象obj对应的类Foo中的 __iter__方法,并获取返回值
    #2、循环返回中的对象
    class Foo:
        def __init__(self,name,age):
            self.name=name
            self.age=age
        def __iter__(self):
            return iter([11,22,33])
    
    
    obj = Foo('亚瑟', 12)
    #如果类中有__iter__方法,对象=》可迭代对象
    #对象.__iter__()的返回值:迭代器
    #  for 循环,迭代器,next
    #  for循环,可迭代对象,对象.__iter__(),迭代器,next
    #1、执行对象obj对应的类Foo中的 __iter__方法,并获取返回值
    #2、循环返回中的对象
    for i in obj:
        print(i)
    

     For循环语法内部 

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    obj = iter([11,22,33,44])
    
    while True:
        val = obj.next()
        print val

    __module__ 和  __class__ 

      __module__ 表示当前操作的对象在那个模块

      __class__     表示当前操作的对象的类是什么

    1 #!/usr/bin/env python
    2 # -*- coding:utf-8 -*-
    3 
    4 class C:
    5 
    6     def __init__(self):
    7         pass
    lib/aa.py
    1 from lib.aa import C
    2 
    3 obj = C()
    4 print obj.__module__  # 输出 lib.aa,即:输出模块
    5 print obj.__class__      # 输出 lib.aa.C,即:输出类

    __getslice__、__setslice__、__delslice__

     该三个方法用于分片操作,如:列表

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
     
    class Foo(object):
     
        def __getslice__(self, i, j):
            print '__getslice__',i,j
     
        def __setslice__(self, i, j, sequence):
            print '__setslice__',i,j
     
        def __delslice__(self, i, j):
            print '__delslice__',i,j
     
    obj = Foo()
     
    obj[-1:1]                   # 自动触发执行 __getslice__
    obj[0:1] = [11,22,33,44]    # 自动触发执行 __setslice__
    del obj[0:2]                # 自动触发执行 __delslice__

    __new__ 和 __metaclass__

    1 class Foo(object):
    2  
    3     def __init__(self):
    4         pass
    5  
    6 obj = Foo()   # obj是通过Foo类实例化的对象

    上述代码中,obj 是通过 Foo 类实例化的对象,其实,不仅 obj 是一个对象,Foo类本身也是一个对象,因为在Python中一切事物都是对象

    如果按照一切事物都是对象的理论:obj对象是通过执行Foo类的构造方法创建,那么Foo类对象应该也是通过执行某个类的 构造方法 创建。

    print type(obj) # 输出:<class '__main__.Foo'>     表示,obj 对象由Foo类创建
    print type(Foo) # 输出:<type 'type'>              表示,Foo类对象由 type 类创建
    

      

    所以,obj对象是Foo类的一个实例Foo类对象是 type 类的一个实例,即:Foo类对象 是通过type类的构造方法创建。

    那么,创建类就可以有两种方式:

    a). 普通方式

    class Foo(object):
     
        def func(self):
            print ('hello world')
    

    b).特殊方式(type类的构造函数)

    def func(self):
        print ('hello world')
     
    Foo = type('Foo',(object,), {'func': func})
    #type第一个参数:类名
    #type第二个参数:当前类的基类
    #type第三个参数:类的成员

    ==》 类 是由 type 类实例化产生

    那么问题来了,类默认是由 type 类实例化产生,type类中如何实现的创建类?类又是如何创建对象?

    答:类中有一个属性 __metaclass__,其用来表示该类由 谁 来实例化创建,所以,我们可以为 __metaclass__ 设置一个type类的派生类,从而查看 类 创建的过程。

     1 class MyType(type):
     2 
     3     def __init__(self, what, bases=None, dict=None):
     4         super(MyType, self).__init__(what, bases, dict)
     5 
     6     def __call__(self, *args, **kwargs):
     7         obj = self.__new__(self, *args, **kwargs)
     8 
     9         self.__init__(obj)
    10 
    11 class Foo(object):
    12 
    13     __metaclass__ = MyType
    14 
    15     def __init__(self, name):
    16         self.name = name
    17 
    18     def __new__(cls, *args, **kwargs):
    19         return object.__new__(cls, *args, **kwargs)
    20 
    21 # 第一阶段:解释器从上到下执行代码创建Foo类
    22 # 第二阶段:通过Foo类创建obj对象
    23 obj = Foo()
    View Code


    万物皆对象  

    __metaclass__ 类的祖宗

     



     

    1 # class Foo:
    2 #     def func(self):
    3 #         print('hello,world!')
    4 ###################################
    5 def func(self):
    6     print('hello,world!')
    7 Foo=type('Foo',(object,),{'fn':func})
    8 # obj=Foo()
    9 # obj.fn()
    代码
     1 class MyType(type):
     2     def __init__(self,*args,**kwargs):
     3         print('如果创建类会继承type的话,那么类Foo一被创建,就会执行这句')
     4     def __call__(self, *args, **kwargs):
     5         print('234')
     6         r=self.__new__()
     7 
     8 #原先,当我们创建类 Foo 的时候,会默认调用type
     9 #现在,当我们想要让Foo在创建的时候,调用MyType
    10 #
    11 class Foo(object,metaclass=MyType):
    12     def __init__(self):
    13         pass
    14     def __new__(cls, *args, **kwargs):
    15         return "duxiiang"
    16 
    17     def func(self):
    18         print('hello,world!')
    19 
    20 obj=Foo()
    21 '''
    22 Foo=MyType('Foo',(object,),{'fn':func})
    23 1、由于Foo 是 MyType的对象,所以,Foo()执行MyType的__call__方法
    24 2、MyType的__call__方法调用Foo类的__new__方法创建对象obj,并把对象返回
    25 3、返回的值,调用Foo类的__init__方法
    26 
    27 所以表面上看到,当创建obj时,会调用Foo类的__init__方法
    28 '''
      1 #!/usr/bin/env python3
      2 #-*- coding:utf-8 -*-
      3 '''
      4 Administrator
      5 2018/7/24
      6 '''
      7 class MyType(type):
      8     def __init__(self,*args,**kwargs):
      9         print('如果创建类会继承type的话,那么类Foo一被创建,就会执行这句')
     10     def __call__(self, *args, **kwargs):
     11         print('234')
     12         r=self.__new__()
     13 
     14 #原先,当我们创建类 Foo 的时候,会默认调用type
     15 #现在,当我们想要让Foo在创建的时候,调用MyType
     16 #
     17 class Foo(object,metaclass=MyType):
     18     def __init__(self):
     19         pass
     20     def __new__(cls, *args, **kwargs):
     21         return "duxiiang"
     22 
     23     def func(self):
     24         print('hello,world!')
     25 
     26 obj=Foo()
     27 '''
     28 Foo=MyType('Foo',(object,),{'fn':func})
     29 1、由于Foo 是 MyType的对象,所以,Foo()执行MyType的__call__方法
     30 2、MyType的__call__方法调用Foo类的__new__方法创建对象obj,并把对象返回
     31 3、返回的值,调用Foo类的__init__方法
     32 
     33 所以表面上看到,当创建obj时,会调用Foo类的__init__方法
     34 '''
     35 
     36 
     37 
     38 ###################################
     39 # def func(self):
     40 #     print('hello,world!')
     41 # Foo=type('Foo',(object,),{'fn':func})
     42 # obj=Foo()
     43 # obj.fn()
     44 
     45 
     46 
     47 
     48 
     49 
     50 
     51 # class Foo:
     52 #     def __init__(self,name,age):
     53 #         self.name=name
     54 #         self.age=age
     55 #     def __iter__(self):
     56 #         return iter([11,22,33])
     57 #
     58 #
     59 # obj = Foo('亚瑟', 12)
     60 # #如果类中有__iter__方法,对象=》可迭代对象
     61 # #对象.__iter__()的返回值:迭代器
     62 # #  for 循环,迭代器,next
     63 # #  for循环,可迭代对象,对象.__iter__(),迭代器,next
     64 # #1、执行对象obj对应的类Foo中的 __iter__方法,并获取返回值
     65 # #2、循环返回中的对象
     66 # for i in obj:
     67 #     print(i)
     68 
     69 
     70 
     71 # li=list([1,2,3,6,5,7,8])
     72 # r1=li[3]
     73 # print(r1)
     74 
     75 
     76 # class Foo:
     77 #     def __init__(self,name,age):
     78 #         self.name=name
     79 #         self.age=age
     80 #     def __getitem__(self, item):
     81 #         print(item,type(item))
     82 #         # return item+10
     83 #         if type(item)==slice:
     84 #             print('切片处理')
     85 #             print(item.start)
     86 #             print(item.step)
     87 #             print(item.stop)
     88 #         else:
     89 #
     90 #             print("索引处理")
     91 #     def __setitem__(self, key, value):
     92 #         print(key,value)
     93 #     def __delitem__(self, key):
     94 #         print(key)
     95 #
     96 # obj=Foo('亚瑟',12)
     97 # r=obj[8]#在对象后面后加上[] 以索引的方式访问,会自动执行  __getitem__方法
     98 # print(r)
     99 #
    100 # obj[50]='得得得得得得'#通过索引赋值的访问,会自动执行  __setitem__ 方法
    101 #
    102 # del obj[50]
    103 # obj[123]
    104 # obj[1:4:2]
    105 
    106 
    107 
    108 # class Foo:
    109 #     '''
    110 #     当前的简要说明,参数设置等信息
    111 #     '''
    112 #     def __init__(self,name,age):
    113 #         self.name=name
    114 #         self.age=age
    115 #
    116 #     def __add__(self, other):
    117 #         #self == obj('亚瑟',12)
    118 #         #other==obj('后羿',23)
    119 #         # return self.age+other.age
    120 #         return Foo(self.name,other.age)
    121 #     def __del__(self):
    122 #         print('析构方法,对象销毁时,自动执行')
    123 
    124 # obj=Foo('亚瑟',12)
    125 # d=obj.__dict__
    126 # print(d)
    127 # print(Foo.__dict__)
    128 
    129 # obj2=Foo('后羿',23)
    130 #
    131 # r=obj+obj2
    132 # #两个相加时,会自动执行第一个对象的 __add__ 方法,并且把第二个参数当做参数传递进去
    133 # print(r,type(r))
    134 # print(r.name)
    135 # print(r.age)
    136 
    137 
    138 # class Foo:
    139 #     def __init__(self,name,age):
    140 #         self.name=name
    141 #         self.age=age
    142 #
    143 #     def __str__(self): #用于打印
    144 #         return '%s----%s'%(self.name,self.age)
    145 #
    146 # obj=Foo('Jach',12)
    147 # #如果直接调用对象打印出来,会默认直接调用 __str__ 方法
    148 # #内部先把'print(obj)'默认转换成>>>'print(str(obj))' 并且打印出来
    149 # print(obj)
    150 
    151 
    152 
    153 # s="123"
    154 # # s1=str('123')
    155 # i=int(s)
    156 # print(i,type(i))
    157 ##############################
    158 # class Foo:
    159 #     def __init__(self):
    160 #         pass
    161 #     def __int__(self):
    162 #         return 111
    163 #     def __str__(self):
    164 #         return 'string'
    165 # obj=Foo()
    166 #
    167 # print(obj,'
    ',type(obj))
    168 #
    169 # #int ,对象,自动执行对象的__int__ 方法,并将返回值赋值给int对象
    170 # r=int(obj)
    171 # print(r)
    172 # #str ,对象,自动执行对象的__str__ 方法,并将返回值赋值给str对象
    173 # t=str(obj)
    174 # print(t)
    175 
    176 
    177 
    178 # class Foo:
    179 #     def __init__(self): #对象后面加上() 自动执行 init 方法
    180 #         print('init')
    181 #
    182 #
    183 #     def __call__(self, *args, **kwargs):  ##对象后面加() 自动执行 call 方法
    184 #         print('call')
    185 #
    186 #
    187 # # obj=Foo()
    188 # # obj()
    189 # Foo()()
    190 
    191 
    192 
    193 # class ClassFather:
    194 #     def __init__(self):
    195 #         self.__age=23
    196 #         self.score=90
    197 #
    198 # class ClassSon(ClassFather):
    199 #     def __init__(self,name):
    200 #         self.name=name
    201 #         self.__gender='男'
    202 #         super().__init__()
    203 #
    204 #     def show(self):
    205 #         print(self.name)
    206 #         print(self.__gender)
    207 #         print(self.score)
    208 #         #print(self.__age)#私有字段只能在类的内部使用,不能被继承
    209 #
    210 # s=ClassSon('李逵')
    211 # s.show()
    212 
    213 # #案例 数据库账户
    214 # class Database:
    215 #     __root="root"
    216 #     __pwd='abc123'
    217 #     __port='3306'
    218 #     __dbname='ssoa'
    219 #     # def __init__(self,pwd):
    220 #     #     pass
    221 # ###############只读模式########################
    222 #     def get_root(self): #只能访问,不能修改  可读
    223 #         return self.__root
    224 #     def get_port(self):
    225 #         return self.__port
    226 #
    227 #     ###########读写模式 #################
    228 #     def get_pwd(self):  #获取密码
    229 #         return self.__pwd
    230 #     def set_pwd(self,pwd):  #修改密码
    231 #         self.__pwd = pwd
    232 #
    233 # db=  Database()
    234 # print(db.get_pwd()) #调用密码
    235 # db.set_pwd('456')   #修改密码
    236 # print(db.get_pwd())#调用密码
    237 
    238 
    239 
    240 
    241 
    242 
    243 # class Foo:
    244 #
    245 #     def gender(self):
    246 #         return '女'
    247 # obj=Foo()
    248 # print(obj.gender())
    249 #####################################
    250 # class Foo:
    251 #
    252 #     def __gender(self):
    253 #         return '女'
    254 #     def get_gender(self):
    255 #         r=self.__gender()
    256 #         return r
    257 # obj=Foo()
    258 # print(obj.get_gender())
    259 
    260 
    261 
    262 # class Foo:
    263 #     __gender='男'
    264 #     def __init__(self):
    265 #         pass
    266 #
    267 #     def get_gender(self):
    268 #         return Foo.__gender
    269 #
    270 #     # @staticmethod
    271 #     # def get_gender():
    272 #     #     return Foo.__gender
    273 # # obj=Foo()
    274 # # print(obj.get_gender())
    275 # print(Foo.get_gender())
    276 
    277 
    278 
    279 
    280 
    281 
    282 #成员修饰符  修饰符可以规定内部的字段、属性、方法等 是共有的成员,私有的成员
    283 # class Foo:
    284 #     #gender='男' #对于静态字段
    285 #     __gender = '男'
    286 #     def __init__(self,name,age):
    287 #         self.name=name
    288 #         #self.age=age  #对于普通字段可以在外部直接访问
    289 #         self.__age=age #添加 __ 两个下划线,就变成私有字段,外部不能访问
    290 #     def get_age(self):
    291 #         return self.__age
    292 #     @staticmethod
    293 #     def get_gender(self):
    294 #         return Foo.__gender
    295 # obj=Foo('Jack',22)
    296 # # print(obj.name)
    297 # # print(obj.get_age())
    298 # print(Foo.get_gender())
    299 
    300 
    301 
    302 # class Foo:
    303 #     def __init__(self):
    304 #         self.list_name=['tom']
    305 #     def bar(self): #普通方法需要创建对象,通过对象调用
    306 #         print('bar')
    307 #     @staticmethod
    308 #     def sta():#不需要参数,普通方法,保存在类中,通过对象调用
    309 #         print('123')
    310 #     @staticmethod#静态方法,可以不用调用对象,保存在类中,直接调用
    311 #     def sts(a,b):
    312 #         print(a,b)
    313 #     @classmethod
    314 #     def classmd(cls):#类方法必须有一个参数,保存在类中,直接调用
    315 #         print(cls)
    316 #         #cls 类名
    317 #         print('classmd')
    318 #     @property
    319 #     def per(self):  #定义像方法,访问像字段。可以有返回值。  叫属性或者特性
    320 #         del self.list_name[0]
    321 #         print(self.list_name)
    322 #     @per.setter
    323 #     def per(self,val):
    324 #         print(val)
    325 #     @per.deleter
    326 #     def per(self):
    327 #         return 'ok'
    328 # # Foo.sta()
    329 # Foo.sts(3,6)
    330 # Foo.classmd()
    331 # obj=Foo()
    332 # obj.per
    333 # obj.per=456
    334 # del obj.per
    335 
    336 
    337 
    338 # class Foo:
    339 #     def f1(self):
    340 #         print('123')
    341 #         return 123
    342 #     def f2(self,val):
    343 #         print(val)
    344 #     def f3(self):
    345 #         print('ok')
    346 #     per=property(fget=f1,fset=f2,fdel=f3)
    347 # ######等价于#################
    348 #     # @property
    349 #     # def per(self):
    350 #     #     return 123
    351 # obj=Foo()
    352 # # obj.per=456
    353 # del obj.per
    354 
    355 
    356 
    357 
    358 # class Province:
    359 #     country='中国' #静态字段,属于类
    360 #     def __init__(self,name):
    361 #         self.name=name  #这个是普通字段,属于对象
    362 #
    363 #
    364 # print(Province.country)
    365 # jiangsu=Province('江苏')
    366 # print(jiangsu.country)
    练习草图
  • 相关阅读:
    tsql导出EXCEL语句待测试
    IE和Firefox下event乱谈
    cf 167.d( 多重集全排列 )
    poj 1815(最小割 + 枚举)
    Codeforces Round #167 (Div. 2)
    poj 3308(最小割+对数处理)
    在FootTemplate与ItemTemplate中间扩展Repeater模板
    在没有高度设定的情况下让margintop生效
    float:left后整体居中
    判断ExecuteScalar()是否返回结果
  • 原文地址:https://www.cnblogs.com/Mengchangxin/p/9365385.html
Copyright © 2011-2022 走看看