python-09

一、面向对象编程OOP
1、OOP是python的一种抽象方法。
2、OPP最好主要的两个方面:类和实例。
3、定义类
>>> class MyData(object): x = 4

4、根据类创建实例
>>> a = MyData()

5、定义类的方法
  1. >>> class MyData(object):
  2. ... def pstar(self):
  3. ... print '*' * 40
  4. ...
  5. >>> a = MyData()
  6. >>> a.pstar()
  7. ****************************************

6、调用方法
a = MyData()
a.x

7、创建类的例子
  1. #!/usr/bin/env python
  2. class AddBookEntry(object):
  3. def __init__(self, nm, em):
  4. self.name = nm
  5. self.email = em
  6. tom = AddBookEntry('tom', 'tom@qq.com')
  7. print tom.name
  8. print tom.email
  9. alice = AddBookEntry('alice', 'alice@163.com')
  10. print alice.name
  11. print alice.email
 

  1. #!/usr/bin/env python
  2. class AddBookEntry(object):
  3. def __init__(self, nm, em):
  4. self.name = nm
  5. self.email = em
  6. def getEmail(self):
  7. print 'get email'
  8. return self.email
  9. def updateEmail(self, newem):
  10. self.email = newem
  11. print 'email upadte ...'
  12. tom = AddBookEntry('tom', 'tom@qq.com')
  13. alice = AddBookEntry('alice', 'alice@163.com')
  14. print alice.email
  15. print alice.getEmail()
  16. alice.updateEmail('alice@qq.com')
  17. print alice.getEmail()
8、 创建一个子类
  1. #!/usr/bin/env python
  2. class AddBookEntry(object):
  3. def __init__(self, nm, em):
  4. self.name = nm
  5. self.email = em
  6. def getEmail(self):
  7. print 'get email'
  8. return self.email
  9. def updateEmail(self, newem):
  10. self.email = newem
  11. print 'email upadte ...'
  12. class EmplAddrBookEntry(AddBookEntry):
  13. def __init__(self, id, nm, em, ph):
  14. AddBookEntry.__init__(self, nm, em)
  15. self.id = id
  16. self.phone = ph
  17. def getPhone(self):
  18. return self.phone
  19. alice = EmplAddrBookEntry('0001', 'alice', 'alice@qq.com', '12345678901')
  20. print alice.getEmail()
  21. print alice.getPhone()
 
  1. #!/usr/bin/env python
  2. class Info(object):
  3. def __init__(self, em, ph):
  4. self.email = em
  5. self.phone = ph
  6. def getEmail(self):
  7. return self.email
  8. def updateEmail(self, newem):
  9. self.email = newem
  10. class Contacts(object):
  11. def __init__(self, nm, em, ph):
  12. self.name = nm
  13. self.info = Info(em, ph)
  14. alice = Contacts('alice', 'alice@qq.com', '12345678901')
  15. print alice.info.getEmail()
  16. alice.info.updateEmail('alice@163.com')
  17. print alice.info.getEmail()
 
对任何类C
C.__name__                类C的名字(字符串)
C.__doc__                    类C的文档字符串
C.__bases__               类C的所有父类构成的元组
C.__dict__                    类C的属性
C.__module__            类C定义所在的模块(1.5版本新增)
C.__class__                实例C对就的类(仅新式类中)
<type 'type'>类和类型统一了

定义一个类来计算这个假想旅馆租房费用。__init__()构造器对一些实例属性进行初始化。
c alcTotal()方法用来决定是计算每日总的租房费用还是计算所有天全部的租房费。
  1. #!/usr/bin/env python
  2. class HoteCacl(object):
  3. def __init__(self, rt, sales = 0.085, rm = 0.1):
  4. self.rt = rt
  5. self.sales = sales
  6. self.rm = rm
  7. def caclTotal(self, days = 1):
  8. return self.rt * (1 + self.sales + self.rm) * days
  9. sfo = HoteCacl(200)
  10. print sfo.caclTotal()
  11. print sfo.caclTotal(3)

核心笔记:self是什么?
self变量用于在类实例方法中引用方法所绑定的实例。因为方法的实例在任何方法调用中总是作为第一个参数传递的,self被选中用来代表实例。你必须在方法声明中放在self(你可能已经注意到了这点),但可以在方法中不使用实例(self)。如果你的方法中没有用到self,那么请考虑创建 一个常规函数,除非你有特别的原因。毕竟,你的方法代码没有使用实例,没有与类关联其功能,这使得它看起来更像一个常规函数。在其它面向对象语言中,self可能被称为this。

11、私有化。只能在对象内部使用的属性、方法
  1. #!/usr/bin/env python
  2. class MyData(object):
  3. def __init__(self, nm):
  4. delf.__name = nm
  5. a = MyData('alice')
  6. print a.__name        //将会报错
为了能访问对象的属性,只能在类的定义里面定义方法
  1. #!/usr/bin/env python
  2. class MyData(object):
  3. def __init__(self, nm):
  4. self.__name = nm
  5. def getName(self):
  6. return self.__name
  7. a = MyData('alice')
  8. print a.getName()
若在PYTHON中以下划线开头的变量显示为                      _类名__变量名

12、判断某一个类是否为另一个类的子类
issubclass(EmplAddrBook, AddrBook)     //返回True

13、查看某个类的父类,可以使用特殊属性:
EmplAddrBook.__bases__

14、判断某一个对象是否为一个类的实例
  1. >>> isinstance(3, int)
  2. True
  3. >>> isinstance(tom, AddrBook)
  4. True

15、多个超类
  1. #!/usr/bin/env python
  2. class A(object):
  3. def hello(self):
  4. print 'hello from A'
  5. class B(object):
  6. def greet(self):
  7. print 'greeting from A'
  8. class C(A, B):
  9. pass
  10. c = C()
  11. c.hello()
  12. c.greet()

二、高级功能模块
1、使用ftplib下载文件

  1. #!/usr/bin/env python
  2. import ftplib
  3. import sys
  4. import socket
  5. def getfile():
  6. try:
  7. f = ftplib.FTP('localhost')
  8. except (socket.gaierror, socket.error), e:
  9. print 'Error: ', e
  10. sys.exit(1)
  11. f.login('ftp')
  12. print f.getwelcome()
  13. f.cwd('pub')
  14. print 'change dir: ', f.pwd()
  15. print 'file list: '
  16. f.dir()
  17. fname = raw_input('file to download: ')
  18. f.retrbinary('RETR %s' % fname, open(fname, 'w').write)
  19. f.quit()
  20. def test():
  21. getfile()
  22. if __name__ == '__main__':
  23. test()






原文地址:https://www.cnblogs.com/fina/p/6197361.html