zoukankan      html  css  js  c++  java
  • 面向对象

    第七章:

    • 面向对象高级语法部分
      • 静态方法、类方法、属性方法
      • 类的特殊方法
      • 反射
    • 异常处理
    • Socket开发基础

    面向对象高级语法部分

    静态方法

    通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的 方法,可以在实例化后直接调用,并且在方法里可以通过self.调用实例变量或类变量,但静态方法是不可以访问实例变量或类变量的,一个不能访问实例变量 和类变量的方法,其实相当于跟类本身已经没什么关系了,它与类唯一的关联就是需要通过类名来调用这个方法

     1 class Dog(object):
     2  
     3     def __init__(self,name):
     4         self.name = name
     5  
     6     @staticmethod #把eat方法变为静态方法
     7     def eat(self):
     8         print("%s is eating" % self.name)
     9  
    10  
    11  
    12 d = Dog("Tom")
    13 d.eat()

    上面的调用会出以下错误,说是eat需要一个self参数,但调用时却没有传递,没错,当eat变成静态方法后,再通过实例调用时就不会自动把实例本身当作一个参数传给self了

    1 <span style="color: #ff0000;">Traceback (most recent call last):
    2   File "/PycharmProjects/python基础/自动化day7面向对象高级/静态方法.py", line 17, in <module>
    3     d.eat()
    4 TypeError: eat() missing 1 required positional argument: 'self'
    5 </span>

    想让上面的代码可以正常工作有两种办法

    1. 调用时主动传递实例本身给eat方法,即d.eat(d) 

    2. 在eat方法中去掉self参数,但这也意味着,在eat中不能通过self.调用实例中的其它变量了

     1 1 class Dog(object):
     2  2 
     3  3     def __init__(self,name):
     4  4         self.name = name
     5  5 
     6  6     @staticmethod
     7  7     def eat():
     8  8         print(" is eating")
     9  9 
    10 10 
    11 11 
    12 12 d = Dog("Tom")
    13 13 d.eat()

    类方法

    类方法通过@classmethod装饰器实现,类方法和普通方法的区别是, 类方法只能访问类变量,不能访问实例变量

     1 class Dog(object):
     2     def __init__(self,name):
     3         self.name = name
     4  
     5     @classmethod
     6     def eat(self):
     7         print("%s is eating" % self.name)
     8  
     9  
    10 d = Dog("Tom")
    11 d.eat()

    执行报错如下,说Dog没有name属性,因为name是个实例变量,类方法是不能访问实例变量的

    1 Traceback (most recent call last):
    2   File "/PycharmProjects/python基础/自动化day7面向对象高级/类方法.py", line 16, in <module>
    3     d.eat()
    4   File "/PycharmProjects/python基础/自动化day7面向对象高级/类方法.py", line 11, in eat
    5     print("%s is eating" % self.name)
    6 AttributeError: type object 'Dog' has no attribute 'name'

    此时可以定义一个类变量,也叫name,看下执行效果

     1 class Dog(object):
     2     name = "我是类变量"
     3     def __init__(self,name):
     4         self.name = name
     5  
     6     @classmethod
     7     def eat(self):
     8         print("%s is eating" % self.name)
     9  
    10  
    11  
    12 d = Dog("Tom")
    13 d.eat()
    14  
    15  
    16 #执行结果
    17  
    18 我是类变量 is eating

    属性方法

    属性方法的作用就是通过@property把一个方法变成一个静态属性

     1 class Dog(object):
     2  
     3     def __init__(self,name):
     4         self.name = name
     5  
     6     @property
     7     def eat(self):
     8         print(" %s is eating" %self.name)
     9  
    10  
    11 d = Dog("Tom")
    12 d.eat()

    调用会出以下错误, 说NoneType is not callable, 因为eat此时已经变成一个静态属性了, 不是方法了, 想调用已经不需要加()号了,直接d.eat就可以了

    1 Traceback (most recent call last):
    2  ChenRonghua is eating
    3   File "/PycharmProjects/python基础/自动化day7面向对象高级/属性方法.py", line 16, in <module>
    4     d.eat()
    5 TypeError: 'NoneType' object is not callable

    正常调用如下

    d = Dog("Tom")
    d.eat
     
    输出
    Tom is eating

    把一个方法变成静态属性有什么卵用呢?既然想要静态变量,那直接定义成一个静态变量不就得了么?well, 以后你会需到很多场景是不能简单通过 定义 静态属性来实现的, 比如 ,你想知道一个航班当前的状态,是到达了、延迟了、取消了、还是已经飞走了, 想知道这种状态你必须经历以下几步:

    1. 连接航空公司API查询

    2. 对查询结果进行解析 

    3. 返回结果给你的用户

    因此这个status属性的值是一系列动作后才得到的结果,所以你每次调用时,其实它都要经过一系列的动作才返回你结果,但这些动作过程不需要用户关心, 用户只需要调用这个属性就可以,明白 了么?

     1 class Flight(object):
     2     def __init__(self,name):
     3         self.flight_name = name
     4 
     5 
     6     def checking_status(self):
     7         print("checking flight %s status " % self.flight_name)
     8         return  1
     9 
    10     @property
    11     def flight_status(self):
    12         status = self.checking_status()
    13         if status == 0 :
    14             print("flight got canceled...")
    15         elif status == 1 :
    16             print("flight is arrived...")
    17         elif status == 2:
    18             print("flight has departured already...")
    19         else:
    20             print("cannot confirm the flight status...,please check later")
    21 
    22 
    23 f = Flight("CZ323")
    24 f.flight_status

    cool , 那现在我只能查询航班状态, 既然这个flight_status已经是个属性了, 那我能否给它赋值呢?试试吧

    1 f = Flight("CZ323")
    2 f.flight_status
    3 f.flight_status =  2
    1 checking flight CA980 status
    2 flight is arrived...
    3 Traceback (most recent call last):
    4   File "/PycharmProjects/python基础/自动化day7面向对象高级/属性方法.py", line 58, in <module>
    5     f.flight_status =  2
    6 AttributeError: can't set attribute

    当然可以改, 不过需要通过@proerty.setter装饰器再装饰一下,此时 你需要写一个新方法, 对这个flight_status进行更改。

     1 class Flight(object):
     2     def __init__(self,name):
     3         self.flight_name = name
     4 
     5 
     6     def checking_status(self):
     7         print("checking flight %s status " % self.flight_name)
     8         return  1
     9 
    10 
    11     @property
    12     def flight_status(self):
    13         status = self.checking_status()
    14         if status == 0 :
    15             print("flight got canceled...")
    16         elif status == 1 :
    17             print("flight is arrived...")
    18         elif status == 2:
    19             print("flight has departured already...")
    20         else:
    21             print("cannot confirm the flight status...,please check later")
    22     
    23     @flight_status.setter #修改
    24     def flight_status(self,status):
    25         status_dic = {
    26             0 : "canceled",
    27             1 :"arrived",
    28             2 : "departured"
    29         }
    30         print("33[31;1mHas changed the flight status to 33[0m",status_dic.get(status) )
    31 
    32     @flight_status.deleter  #删除
    33     def flight_status(self):
    34         print("status got removed...")
    35 
    36 f = Flight("CA980")
    37 f.flight_status
    38 f.flight_status =  2 #触发@flight_status.setter 
    39 del f.flight_status #触发@flight_status.deleter 
    View Code

    注意以上代码里还写了一个@flight_status.deleter, 是允许可以将这个属性删除

    类的特殊成员方法

    1.__doc__ 表示类的描述信息

    1 class Foo:
    2     """ 描述类信息,这是用于看片的神奇 """
    3  
    4     def func(self):
    5         pass
    6  
    7 print Foo.__doc__
    8 #输出:类的描述信息
    View Code

    2.__module__ 和__class__

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

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

    1 class C:
    2 
    3     def __init__(self):
    4         self.name = 'wupeiqi'
    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,即:输出类
    index.py

    3.__init__ 构造方法,通过类创建对象时,自动触发执行.

    4.__del__ 析构方法

       析构方法,当对象在内存中被释放时,自动触发执行。

    注:此方法一般无须定义,因为Python是一门高级语言,程序员在使用时无需关心内存的分配和释放,因为此工作都是交给Python解释器来执行,所以,析构函数的调用是由解释器在进行垃圾回收时自动触发执行的

     5. __call__ 对象后面加括号,触发执行。

    注:构造方法的执行是由创建对象触发的,即:对象 = 类名() ;而对于 __call__ 方法的执行是由对象后加括号触发的,即:对象() 或者 类()()

     1 class Foo:
     2  
     3     def __init__(self):
     4         pass
     5      
     6     def __call__(self, *args, **kwargs):
     7  
     8         print '__call__'
     9  
    10  
    11 obj = Foo() # 执行 __init__
    12 obj()       # 执行 __call__
    View Code

    6.__dict__ 查看类或对象中的所有成员

     1 class Province:
     2  
     3     country = 'China'
     4  
     5     def __init__(self, name, count):
     6         self.name = name
     7         self.count = count
     8  
     9     def func(self, *args, **kwargs):
    10         print 'func'
    11  
    12 # 获取类的成员,即:静态字段、方法、
    13 print Province.__dict__
    14 # 输出:{'country': 'China', '__module__': '__main__', 'func': <function func at 0x10be30f50>, '__init__': <function __init__ at 0x10be30ed8>, '__doc__': None}
    15  
    16 obj1 = Province('HeBei',10000)
    17 print obj1.__dict__
    18 # 获取 对象obj1 的成员
    19 # 输出:{'count': 10000, 'name': 'HeBei'}
    20  
    21 obj2 = Province('HeNan', 3888)
    22 print obj2.__dict__
    23 # 获取 对象obj1 的成员
    24 # 输出:{'count': 3888, 'name': 'HeNan'}
    View Code

    7.__str__如果一个类中定义了__str__方法,那么在打印对象时,默认输出该方法的返回值.

    1 class Foo:
    2  
    3     def __str__(self):
    4         return 'tom'
    5  
    6  
    7 obj = Foo()
    8 print obj
    9 # 输出:tom
    View Code

    8.__getitem__、__setitem__、__delitem__

    用于索引操作,如字典。以上分别表示获取、设置、删除数据

     1 class Foo(object):
     2  
     3     def __getitem__(self, key):
     4         print('__getitem__',key)
     5  
     6     def __setitem__(self, key, value):
     7         print('__setitem__',key,value)
     8  
     9     def __delitem__(self, key):
    10         print('__delitem__',key)
    11  
    12  
    13 obj = Foo()
    14  
    15 result = obj['k1']      # 自动触发执行 __getitem__
    16 obj['k2'] = 'alex'   # 自动触发执行 __setitem__
    17 del obj['k1']    
    View Code

    9.__new__ __metaclass__

    1 class Foo(object):
    2  
    3  
    4     def __init__(self,name):
    5         self.name = name
    6  
    7  
    8 f = Foo("alex")
    View Code

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

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

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

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

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

    a). 普通方式

    1 class Foo(object):
    2   
    3     def func(self):
    4         print 'hello 'Tom'

    b). 特殊方式

    def func(self):
        print 'hello Tom'
      
    Foo = type('Foo',(object,), {'func': func})
    #type第一个参数:类名
    #type第二个参数:当前类的基类
    #type第三个参数:类的成员
     1 def func(self):
     2     print("hello %s"%self.name)
     3 
     4 def __init__(self,name,age):
     5     self.name = name
     6     self.age = age
     7 Foo = type('Foo',(object,),{'func':func,'__init__':__init__})
     8 
     9 f = Foo("jack",22)
    10 f.func()
    View Code

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

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

     1 #_*_coding:utf-8_*_
     2 __author__ = 'Alex Li'
     3  
     4  
     5 class MyType(type):
     6  
     7     def __init__(self, what, bases=None, dict=None):
     8         print("--MyType init---")
     9         super(MyType, self).__init__(what, bases, dict)
    10  
    11     def __call__(self, *args, **kwargs):
    12         print("--MyType call---")
    13         obj = self.__new__(self, *args, **kwargs)
    14  
    15         self.__init__(obj, *args, **kwargs)
    16  
    17  
    18 class Foo(object):
    19  
    20     __metaclass__ = MyType
    21  
    22     def __init__(self, name):
    23         self.name = name
    24         print("Foo ---init__")
    25  
    26     def __new__(cls, *args, **kwargs):
    27         print("Foo --new--")
    28         return object.__new__(cls)
    29  
    30  
    31 # 第一阶段:解释器从上到下执行代码创建Foo类
    32 # 第二阶段:通过Foo类创建obj对象
    33 obj = Foo("Alex")
    View Code

    metaclass 详解文章:http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python 得票最高那个答案写的非常好

    反射

    通过字符串映射或修改程序运行时的状态、属性、方法, 有以下4个方法

    1 def getattr(object, name, default=None): # known special case of getattr
    2     """
    3     getattr(object, name[, default]) -> value
    4     
    5     Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.
    6     When a default argument is given, it is returned when the attribute doesn't
    7     exist; without it, an exception is raised in that case.
    8     """
    9     pass
    getattr(object, name, default=None)
    1 判断object中有没有一个name字符串对应的方法或属性
    hasattr(object,name)
    1 def setattr(x, y, v): # real signature unknown; restored from __doc__
    2     """
    3     Sets the named attribute on the given object to the specified value.
    4     
    5     setattr(x, 'y', v) is equivalent to ``x.y = v''
    setattr(x, y, v)
    1 def delattr(x, y): # real signature unknown; restored from __doc__
    2     """
    3     Deletes the named attribute from the given object.
    4     
    5     delattr(x, 'y') is equivalent to ``del x.y''
    6     """
    delattr(x, y)
     1 class Foo(object):
     2  
     3     def __init__(self):
     4         self.name = 'wupeiqi'
     5  
     6     def func(self):
     7         return 'func'
     8  
     9 obj = Foo()
    10  
    11 # #### 检查是否含有成员 ####
    12 hasattr(obj, 'name')
    13 hasattr(obj, 'func')
    14  
    15 # #### 获取成员 ####
    16 getattr(obj, 'name')
    17 getattr(obj, 'func')
    18  
    19 # #### 设置成员 ####
    20 setattr(obj, 'age', 18)
    21 setattr(obj, 'show', lambda num: num + 1)
    22  
    23 # #### 删除成员 ####
    24 delattr(obj, 'name')
    25 delattr(obj, 'func')
    反射示例

    动态导入模块

    1 import importlib
    2  
    3 __import__('import_lib.metaclass') #这是解释器自己内部用的
    4 #importlib.import_module('import_lib.metaclass') #与上面这句效果一样,官方建议用这个
    View Code

    异常处理

    参考 http://www.cnblogs.com/wupeiqi/articles/5017742.html 

    1、异常基础

    在编程过程中为了增加友好性,在程序出现bug时一般不会将错误信息显示给用户,而是现实一个提示的页面,通俗来说就是不让用户看见大黄页!!!

    try:
        pass
    except Exception,ex:
        pass

    需求:将用户输入的两个数字相加

     1 while True:
     2     num1 = raw_input('num1:')
     3     num2 = raw_input('num2:')
     4     try:
     5         num1 = int(num1)
     6         num2 = int(num2)
     7         result = num1 + num2
     8     except Exception, e:
     9         print '出现异常,信息如下:'
    10         print e
    View Code

    2、异常种类

    python中的异常种类非常多,每个异常专门用于处理某一项异常!!!

     1 AttributeError 试图访问一个对象没有的树形,比如foo.x,但是foo没有属性x
     2 IOError 输入/输出异常;基本上是无法打开文件
     3 ImportError 无法引入模块或包;基本上是路径问题或名称错误
     4 IndentationError 语法错误(的子类) ;代码没有正确对齐
     5 IndexError 下标索引超出序列边界,比如当x只有三个元素,却试图访问x[5]
     6 KeyError 试图访问字典里不存在的键
     7 KeyboardInterrupt Ctrl+C被按下
     8 NameError 使用一个还未被赋予对象的变量
     9 SyntaxError Python代码非法,代码不能编译(个人认为这是语法错误,写错了)
    10 TypeError 传入对象类型与要求的不符合
    11 UnboundLocalError 试图访问一个还未被设置的局部变量,基本上是由于另有一个同名的全局变量,
    12 导致你以为正在访问它
    13 ValueError 传入一个调用者不期望的值,即使值的类型是正确的
    常用异常
    ArithmeticError
    AssertionError
    AttributeError
    BaseException
    BufferError
    BytesWarning
    DeprecationWarning
    EnvironmentError
    EOFError
    Exception
    FloatingPointError
    FutureWarning
    GeneratorExit
    ImportError
    ImportWarning
    IndentationError
    IndexError
    IOError
    KeyboardInterrupt
    KeyError
    LookupError
    MemoryError
    NameError
    NotImplementedError
    OSError
    OverflowError
    PendingDeprecationWarning
    ReferenceError
    RuntimeError
    RuntimeWarning
    StandardError
    StopIteration
    SyntaxError
    SyntaxWarning
    SystemError
    SystemExit
    TabError
    TypeError
    UnboundLocalError
    UnicodeDecodeError
    UnicodeEncodeError
    UnicodeError
    UnicodeTranslateError
    UnicodeWarning
    UserWarning
    ValueError
    Warning
    ZeroDivisionError
    
    更多异常
    更多异常
    1 dic = ["wupeiqi", 'alex']
    2 try:
    3     dic[10]
    4 except IndexError, e:
    5     print e
    IndexError
    dic = {'k1':'v1'}
    try:
        dic['k20']
    except KeyError, e:
        print e
    IndexError
    1 s1 = 'hello'
    2 try:
    3     int(s1)
    4 except ValueError, e:
    5     print e
    ValueError

    对于上述实例,异常类只能用来处理指定的异常情况,如果非指定异常则无法处理。

    1 # 未捕获到异常,程序直接报错
    2  
    3 s1 = 'hello'
    4 try:
    5     int(s1)
    6 except IndexError,e:
    7     print e

    所以,写程序时需要考虑到try代码块中可能出现的任意异常,可以这样写:

    1 s1 = 'hello'
    2 try:
    3     int(s1)
    4 except IndexError,e:
    5     print e
    6 except KeyError,e:
    7     print e
    8 except ValueError,e:
    9     print e

    万能异常 在python的异常中,有一个万能异常:Exception,他可以捕获任意异常,即

    1 s1 = 'hello'
    2 try:
    3     int(s1)
    4 except Exception,e:
    5     print e

    接下来你可能要问了,既然有这个万能异常,其他异常是不是就可以忽略了!

    答:当然不是,对于特殊处理或提醒的异常需要先定义,最后定义Exception来确保程序正常运行

    1 s1 = 'hello'
    2 try:
    3     int(s1)
    4 except KeyError,e:
    5     print '键错误'
    6 except IndexError,e:
    7     print '索引错误'
    8 except Exception, e:
    9     print '错误'

    3、异常其他结构

     1 try:
     2     # 主代码块
     3     pass
     4 except KeyError,e:
     5     # 异常时,执行该块
     6     pass
     7 else:
     8     # 主代码块执行完,执行该块
     9     pass
    10 finally:
    11     # 无论异常与否,最终执行该块
    12     pass

    4、主动触发异常

    1 try:
    2     raise Exception('错误了。。。')
    3 except Exception,e:
    4     print e

    5、自定义异常

    class TomException(Exception):
     
        def __init__(self, msg):
            self.message = msg
     
        def __str__(self):
            return self.message
     
    try:
        raise TomException('我的异常')
    except TomException,e:
        print e

    socket编程

    参考:http://www.cnblogs.com/wupeiqi/articles/5040823.html

    socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求。

    socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,对于文件用【打开】【读写】【关闭】模式来操作。 socket就是该模式的一个实现,socket即是一种特殊的文件,一些socket函数就是对其进行的操作(读/写IO、打开、关闭)

    socket和file的区别:

    • file模块是针对某个指定文件进行【打开】【读写】【关闭】
    • socket模块是针对 服务器端 和 客户端Socket 进行【打开】【读写】【关闭】

     

     1 #!/usr/bin/env python
     2 # -*- coding:utf-8 -*-
     3 
     4 import socket
     5 
     6 ip_port = ('127.0.0.1',9999)
     7 
     8 sk = socket.socket()
     9 sk.bind(ip_port)
    10 sk.listen(5)
    11 
    12 while True:
    13     print 'server waiting...'
    14     conn,addr = sk.accept()
    15 
    16     client_data = conn.recv(1024)
    17     print client_data
    18     conn.sendall('不要回答,不要回答,不要回答')
    19 
    20     conn.close()
    socket sever
     1 #!/usr/bin/env python
     2 # -*- coding:utf-8 -*-
     3 import socket
     4 ip_port = ('127.0.0.1',9999)
     5 
     6 sk = socket.socket()
     7 sk.connect(ip_port)
     8 
     9 sk.sendall('请求占领地球')
    10 
    11 server_reply = sk.recv(1024)
    12 print server_reply
    13 
    14 sk.close()
    15 
    16 复制代码
    socket client

    WEB服务应用:

     1 #!/usr/bin/env python
     2 #coding:utf-8
     3 import socket
     4  
     5 def handle_request(client):
     6     buf = client.recv(1024)
     7     client.send("HTTP/1.1 200 OK
    
    ")
     8     client.send("Hello, World")
     9  
    10 def main():
    11     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    12     sock.bind(('localhost',8080))
    13     sock.listen(5)
    14  
    15     while True:
    16         connection, address = sock.accept()
    17         handle_request(connection)
    18         connection.close()
    19  
    20 if __name__ == '__main__':
    21   main()
    A wise man thinks all that he says, a fool says all that he thinks.
  • 相关阅读:
    014-CallbackServlet代码
    PaymentServlet代码
    013-PaymentUtils工具类模板
    案例30-在线支付
    案例29-购物车提交订单
    案例28-清空购物车
    案例27-购物车删除单一商品
    案例26-购物车
    案例25-servlet的抽取
    在Eclipse或工作空间中 ,复制或修改项目后,把项目部署后发现还是原来的项目名称
  • 原文地址:https://www.cnblogs.com/BernieChen/p/5853157.html
Copyright © 2011-2022 走看看