zoukankan      html  css  js  c++  java
  • Python笔记——类定义

    http://blog.csdn.net/wklken/article/details/6313265

    http://blog.csdn.net/on_1y/article/details/8640012

    一、类定义:

    class <类名>:

        <语句>

    类实例化后,可以使用其属性,实际上,创建一个类之后,可以通过类名访问其属性

    如果直接使用类名修改其属性,那么将直接影响到已经实例化的对象

    类的私有属性:

    __private_attrs  两个下划线开头,声明该属性为私有,不能在类地外部被使用或直接访问

    在类内部的方法中使用时 self.__private_attrs

     类的方法

    在类地内部,使用def关键字可以为类定义一个方法,与一般函数定义不同,类方法必须包含参数self,且为第一个参数

    私有的类方法

    __private_method 两个下划线开头,声明该方法为私有方法,不能在类地外部调用

    在类的内部调用slef.__private_methods

    类的专有方法:

    __init__  构造函数,在生成对象时调用

    __del__   析构函数,释放对象时使用

    __repr__ 打印,转换

    __setitem__按照索引赋值

    __getitem__按照索引获取值

    __len__获得长度

    __cmp__比较运算

    __call__函数调用

    __add__加运算

    __sub__减运算

    __mul__乘运算

    __div__除运算

    __mod__求余运算

    __pow__称方

    示例:

    [python] view plain copy
     
    1. #类定义  
    2. class people:  
    3.     #定义基本属性  
    4.     name = ''  
    5.     age = 0  
    6.     #定义私有属性,私有属性在类外部无法直接进行访问  
    7.     __weight = 0  
    8.     #定义构造方法  
    9.     def __init__(self,n,a,w):  
    10.         self.name = n  
    11.         self.age = a  
    12.         self.__weight = w  
    13.     def speak(self):  
    14.         print("%s is speaking: I am %d years old" %(self.name,self.age))  
    15.   
    16.   
    17. p = people('tom',10,30)  
    18. p.speak()  

    二、继承类定义:

    1.单继承

    class <类名>(父类名)

       <语句>

    eg.

    class childbook(book)

        age = 10

    [python] view plain copy
     
    1. #单继承示例  
    2. class student(people):  
    3.     grade = ''  
    4.     def __init__(self,n,a,w,g):  
    5.         #调用父类的构函  
    6.         people.__init__(self,n,a,w)  
    7.         self.grade = g  
    8.     #覆写父类的方法  
    9.     def speak(self):  
    10.         print("%s is speaking: I am %d years old,and I am in grade %d"%(self.name,self.age,self.grade))  
    11.   
    12.   
    13.   
    14. s = student('ken',20,60,3)  
    15. s.speak()  

    2.类的多重继承

    class 类名(父类1,父类2,....,父类n)

         <语句1>

    需要注意圆括号中父类的顺序,若是父类中有相同的方法名,而在子类使用时未指定,python从左至右搜索

    即方法在子类中未找到时,从左到右查找父类中是否包含方法

    [python] view plain copy
     
    1. #另一个类,多重继承之前的准备  
    2. class speaker():  
    3.     topic = ''  
    4.     name = ''  
    5.     def __init__(self,n,t):  
    6.         self.name = n  
    7.         self.topic = t  
    8.     def speak(self):  
    9.         print("I am %s,I am a speaker!My topic is %s"%(self.name,self.topic))  
    10.   
    11. #多重继承  
    12. class sample(speaker,student):  
    13.     a =''  
    14.     def __init__(self,n,a,w,g,t):  
    15.         student.__init__(self,n,a,w,g)  
    16.         speaker.__init__(self,n,t)  
    17.   
    18. test = sample("Tim",25,80,4,"Python")  
    19. test.speak()#方法名同,默认调用的是在括号中排前地父类的方法  

    类方法的覆写——子类覆盖掉父类的方法

    def 方法名与父类一致

    若是在方法中要使用到父类方法 父类名.方法名

    若是将类放到了模块中

    使用时

    import A

    l = A.类()

    三、私有变量和类局部引用

    实例的私有变量只能在对象内部使用,python中常常使用例如 _ spam 的形式来代表API的非公有部分,无论是函数,方法还是 数据成员。类私有成员的特性的一种有效的用法是可以避免与子类中定义的名字冲突,这种机制叫做 mangling:

    class Mapping:
        def __init__(self, iterable):
            self.items_list = []
            self.__update(iterable)
    
        def update(self, iterable):
            for item in iterable:
                self.items_list.append(item)
    
        __update = update # private copy of original update() method
    
    
    class MappingSubclass(Mapping):
        def update(self, keys, values):
            # provides new signature for update()
            # but does not break __init__()
            for item in zip(keys, values):
                self.items_list.append(item)

    注意上述代码中 __ update 的使用,避免了子类中对update的覆盖影响到基类 __ init__ 中的 update.

    8 结构体

    有时候我们可能需要像C中的struct那样的数据类型,把少量的数据项放在一起。Python中可以使用定义一个空类来实现这一点:

    # filename:p.py
    class Employee:
        pass
    
    john = Employee() # Create an empty employee record
    
    # Fill the fields of the record
    john.name = 'John Doe'
    john.dept = 'computer lab'
    john.salary = 1000
    >>> import p
    >>> p.john
    <p.Employee instance at 0xb71f50ac>
    >>> p.john.name
    'John Doe'
    >>> p.john.dept
    'computer lab'
    >>> p.john.salary
    1000

    9 异常(Exceptions)也是类

    用户定义的异常也可以用类来表示,使用这种机制可以创建出可扩展,层次化的异常。 raise 语句有两种新的形式

    raise Class, instance
    raise instance
    

    第一种形式中,instance必须是Class的一个实例,或者是由它派生出的类。 第二种形式是下面这种形式的缩写

    raise instance.__class__, instance
    

    下面这个例子会依次打印出B,C,D

    class B:
        pass
    class C(B):
        pass
    class D(C):
        pass
    
    for c in [B,C,D]:
        try:
            raise c()
        except D:
            print "D"
        except C:
            print "C"
        except B:
            print "B"
    >>> import f
    B
    C
    D

    10 迭代器

    现在你可能已经注意到了多数容器对象都可以使用for语句来循环

    >>> for elem in [1,2,3]:
    ...     print elem
    ... 
    1
    2
    3
    >>> for elem in (1,2,3):
    ...     print elem
    ... 
    1
    2
    3
    
    

    这一风格清晰,简捷,方便。迭代器的使用在Python中非常普便。for语句的背后,其实是对容器对象调用 iter(). 这个函数返回一个迭代器对象,它定义了next()函数,每次访问容器中的一个元素。当没有元素的时候,next()返回一个 StopIteration异常,告诉for语句循环结束了。

    >>> s = 'asdf'
    >>> it = iter(s)
    >>> it
    <iterator object at 0xb71f590c>
    >>> it.next()
    'a'
    >>> it.next()
    's'
    # q.py
    class Reverse:
        def __init__(self, data):
            self.data = data
            self.index = len(data)
        def __iter__(self):
            return self
        def next(self):
            if self.index == 0:
                raise StopIteration
            self.index = self.index -1
            return self.data[self.index]
    
    >>> import q
    >>> rev = q.Reverse('spam')
    >>> iter(rev)
    <q.Reverse instance at 0xb71f588c>
    >>> for char in rev:
    ...     print char
    ... 
    m
    a
    p
    s
    >>> it.next()
    'd'
    >>> it.next()
    'f'
    >>> it.next()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    StopIteration
    

    理解了迭代机制,就可以很容易地把迭代器加入你的类中,定义__ iter__ ()方法,返回一个有next()方法的对象。 如果一个类定义了next()函数,__ iter__ () 可以仅返回 self:

    11 生成器(Generators)

    生成器是创建迭代器的一个简单而强大的工具。它们像正常函数一样,只是需要返回数据时使用 yield语句。

    # d.py
    def reverse(data):
        for index in range(len(data)-1, -1, -1):
            yield data[index]
    >>> import d
    >>> for char in d.reverse('golf'):
    ...     print char
    ... 
    f
    l
    o
    g
  • 相关阅读:
    [Algorithms] Insertion sort algorithm using TypeScript
    [Algorithms] Determine if a string is a palindrome
    [Algorithm] Determine if two strings are an anagram
    [Vue + TS] Watch for Changes in Vue Using the @Watch Decorator with TypeScript
    [Vue +TS] Use Two-Way Binding in Vue Using @Model Decorator with TypeScript
    uvc摄像头代码解析7
    并查集
    流程节点多场景多表单
    【图像识别】 图像处理和图像分析(leptonica)leptonica-1.68安装配置 (vs2008)
    Eclipse完美汉化教程
  • 原文地址:https://www.cnblogs.com/yuluoxingkong/p/8044422.html
Copyright © 2011-2022 走看看