面向过程:根据业务逻辑从上到下写垒代码

面向对象:对函数进行分类和封装

函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可

类:用来描述具有相同属性和方法的对象的集合,定义了该集合中每个对象共有的属性和方法。

方法重写:如果父类继承的方法不能满足子类的需求,可以对父类进行修改

创建类:

  1. class 类名:
  2. 成员变量
  3. 成员函数

例:

  1. class MyClass: #定义类
  2. n1 = 123456
  3. n2 = 'abc'
  4. def fun(self):
  5. return 'hello python!'
  6. c = MyClass()
  7. print(c.n1) #调用MyClass里面的变量n1
  8. print(c.n2) #调用MyClass里面的变量n2
  9. print(c.fun()) #调用MyClass里面的函数fun()

输出结果:

  1. 123456
  2. abc
  3. hello python!

定义有初始状态的类:类定义了 __init__() 方法的话,类的实例化操作会自动调用 __init__() 方法。__init__() 方法可以有参数,参数通过 __init__() 传递到类的实例化操作上

  1. def __init__(self):
  2. self.data = []

例:

  1. class Province:
  2. a = 123
  3. def __init__(self,name,capital):
  4. self.Name = name
  5. self.Capital = capital
  6.  
  7. hb = Province('河北','石家庄')
  8. hn = Province('河南','郑州')
  9.  
  10. print(hb.Capital)
  11. print(Province.a)

输出结果:

  1. 石家庄
    123

类的使用:

例1:

  1. class information:
  2. name = ''
  3. age = 0
  4. weight = 0
  5. def __init__(self,n,a,w):
  6. self.name = n
  7. self.age = a
  8. self.weight = w
  9. def result(self):
  10. print("%s:I'am %s years old now!"%(self.name,self.age))
  11.  
  12. i = information('pingy',20,130)
  13. print(i.result())

输出结果:

  1. pingyI'am 20 years old now!

例2:

  1. class temp:
  2. def __init__(self):
  3. pass
  4. def __del__(self): #构建函数,在执行结束后都会执行这条函数
  5. print('我要释放了!')
  6. def func(self):
  7. print('go')
  8. def __call__(self):
  9. print('call')
  10.  
  11. f1 = temp()
  12. f1.func() #调用func函数
  13. f1() #相当于执行__call__函数,只是跟普通定义的函数调用方式不一样而已

输出结果:

  1. go
  2. call
  3. 我要释放了!

类的继承:

例1:普通函数继承&继承函数内容并修改内容

  1. class father:
  2. def __init__(self):
  3. pass
  4. def func(self):
  5. print('class father')
  6. def Bad(self):
  7. print('抽烟,喝酒')
  8.  
  9. class son(father): #继承父类
  10. def __init__(self):
  11. pass
  12. def temp(self):
  13. print('class son')
  14. def Bad(self): #在继承父类后并修改父类
  15. print('打篮球')
  16.  
  17. res = son()
  18. res.temp() #调用子类中的函数
  19. res.func() #调用父类中的函数
  20. res.Bad() #修改父类函数的内容

输出结果:

  1. class son
  2. class father
  3. 打篮球

例2:普通函数继承,保存继承函数内容并增加内容

  1. class father:
  2. def __init__(self):
  3. pass
  4. def func(self):
  5. print('class father')
  6. def Bad(self):
  7. print('抽烟,喝酒')
  8.  
  9. class son(father): #继承父类
  10. def __init__(self):
  11. pass
  12. def temp(self):
  13. print('class son')
  14. def Bad(self):
  15. father.Bad(self)
  16. print('打牌') #继承父类中Bad函数,并增加内容
  17.  
  18. res = son()
  19. res.temp() #调用子类中的函数
  20. res.func() #调用父类中的函数
  21. res.Bad() #继承父类中Bad函数,并增加内容

输出结果:

  1. class son
  2. class father
  3. 抽烟,喝酒
  4. 打牌

例3:构建函数的继承

新式类继承方法和旧式类继承方法区别:(如下面两个例子:)

  1. #在2.0版本中:
    >>> class A:
  2. def __init__(self):
  3. print 'A'.center(10,'*')
  4. def save(self):
  5. print 'save from A'
  6.  
  7. >>> class B(A):
  8. def __init__(self):
  9. print 'B'.center(10,'*')
  10.  
  11. >>> class C(A):
  12. def __init__(self):
  13. print 'C'.center(10,'*')
  14. def save(self):
  15. print 'save from C'
  16.  
  17. >>> class D(B,C):
  18. def __init__(self):
  19. print 'D'.center(10,'*')
  20.  
  21. >>> c = D()
  22. ****D*****
  23. >>> c.save()
  24. save from A
  1. #在3.0版本中:
    class A:
  2. def __init__(self):
  3. print('A'.center(10,'*'))
  4. def save(self):
  5. print('save from A')
  6.  
  7. class B(A):
  8. def __init__(self):
  9. print('B'.center(10,'*'))
  10.  
  11. class C(A):
  12. def __init__(self):
  13. print('C'.center(10,'*'))
  14. def save(self):
  15. print('save from C')
  16.  
  17. class D(C,B):
  18. def __init__(self):
  19. print('D'.center(10,'*'))
  20.  
  21. c = D()
  22. c.save()

输出结果:

  1. ****D*****
  2. save from C

例4:旧式类继承方法

  1. class father:
  2. def __init__(self):
  3. print('father.__init__')
  4.  
  5. class son(father): #继承父类
  6. def __init__(self):
  7. print('son.__init__')
  8. print('line'.center(30,'*'))
  9. father.__init__(self) #调用父类构建函数__init__函数
  10.  
  11. res = son()

输出结果:

  1. son.__init__
  2. *************line*************
  3. father.__init__

例5:新式类继承方法

  1. #新式类继承方法
  2. class father(object):
  3. def __init__(self):
  4. print('father.__init__')
  5.  
  6. class son(father): #继承父类
  7. def __init__(self):
  8. print('son.__init__')
  9. print('line'.center(30,'*'))
  10. super(son,self).__init__()
  11.  
  12. res = son()

输出结果:

  1. son.__init__
  2. *************line*************
  3. father.__init__

例6:

  1. class information: #定义基本属性
  2. name = ''
  3. age =0
  4. weight = 0
  5.  
  6. def __init__(self,n,a,w): #定义构造方法
  7. self.name = n
  8. self.age = a
  9. self.weight = w
  10. def res(self):
  11. print("%s: I'm %s years old and the weight is %s" %(self.name,self.age,self.weight))
  12.  
  13. class other(information): #单继承方法
  14. grade = ''
  15. def __init__(self,n,a,w,g):
  16. information.__init__(self,n,a,w)
  17. self.grade = g
  18. def res(self):
  19. print("%s: I'm %s years old ,The weight is %s kg,grade %s" %(self.name,self.age,self.weight,self.grade))
  20.  
  21. c = other('telnet',15,45,8)
  22. print(c.res())

输出结果:

  1. telnet: I'm 15 years old ,the weight is 45 kg,grade 8

多继承:

  1. class father: #创建父类
  2. name = ''
  3. age = 0
  4. def __init__(self,n,a): #定义构造方法
  5. self.name = n
  6. self.age = a
  7. def display(self):
  8. print("姓名:%s,年龄:%s。" %(self.name,self.age))
  9.  
  10. result = father('jay1',10)
  11. print(result.display())
  12.  
  13. print("我是分割线".center(30,'*'))
  14.  
  15. class son1(father): #创建子类1,调用父类
  16. weight = 0
  17. def __init__(self,n,a,w):
  18. self.weight = w
  19. father.__init__(self,n,a)
  20. def display(self):
  21. print("姓名:%s,年龄:%s,体重:%s" %(self.name,self.age,self.weight))
  22.  
  23. result = son1('jay2',20,50)
  24. print(result.display())
  25.  
  26. print("我是分割线".center(30,'+'))
  27.  
  28. class son2(son1): #创建子类2,调用子类1
  29. address = ''
  30. def __init__(self,n,a,w,add):
  31. self.address = add
  32. son1.__init__(self,n,a,w)
  33. def display(self):
  34. print("姓名:%s,年龄:%s,体重:%s,地址:%s。" %(self.name,self.age,self.weight,self.address))
  35.  
  36. result = son2('jay3',30,60,'BeiJing')
  37. print(result.display())
  38.  
  39. print("我是分割线".center(30,'-'))
  40.  
  41. class others: #创建另外一个类others,没有调用任何类
  42. job = ''
  43. def __init__(self,j):
  44. self.job = j
  45. def display(self):
  46. print("工作:%s" %self.job)
  47. result = others('IT')
  48. print(result.display())
  49.  
  50. print("我是分割线".center(30,'='))
  51.  
  52. class another(son2,others): #创建另一个类another,调用子类2和others类,多重调用
  53. email = ''
  54. def __init__(self,n,a,w,add,j,e):
  55. self.email = e
  56. son2.__init__(self,n,a,w,add)
  57. others.__init__(self,j)
  58. def display(self):
  59. print("姓名:%s,年龄:%s,体重:%s,地址:%s,工作:%s,邮件:%s" %(self.name,self.age,self.weight,self.address,self.job,self.email))
  60.  
  61. result = another('jay4',40,70,'shanghai','HR','123@qq.com')
  62. print(result.display())

输出结果:

  1. 姓名:jay1,年龄:10
  2. ************我是分割线*************
  3. 姓名:jay2,年龄:20,体重:50
  4. ++++++++++++我是分割线+++++++++++++
  5. 姓名:jay3,年龄:30,体重:60,地址:BeiJing
  6. ------------我是分割线-------------
  7. 工作:IT
  8. ============我是分割线=============
  9. 姓名:jay4,年龄:40,体重:70,地址:shanghai,工作:HR,邮件:123@qq.com

 类的私有方法:

  1. class Test:
  2. __secretCount = 0 #私有变量
  3. publicCount = 0 #公开变量
  4.  
  5. def counter(self):
  6. self.__secretCount += 1
  7. self.publicCount += 1
  8. print(self.__secretCount)
  9.  
  10. result = Test()
  11. print(result.counter())
  12. print('Line'.center(30,'+'))
  13. print(result.publicCount)
  14. print('Line'.center(30,'*'))
  15. print(result.__secrctCount)

输出结果:

  1. 1
  2. +++++++++++++Line+++++++++++++
  3. 1
  4. *************Line*************
  5. Traceback (most recent call last):
  6. print(result.__secrctCount)
  7. AttributeError: 'Test' object has no attribute '__secrctCount' #报错,私有变量不能访问。

类的专有方法:

  1. class Test:
  2. name = 'pingy'
  3. age = 20
  4. weight = 50
  5. address = 'BeiJing'
  6. res = Test()
  7.  
  8. res.job = 'IT' #新增一个对象和属性
  9. 1.print(res.job)
  10. 2.print(getattr(res,'weight')) #查看对象'weight'的属性
  11. 3.print(hasattr(res,'address')) #查看类中是否存在'address'对象,如果存在返回True,否则返回False
  12. del Test.address #删除address对象
  13. 4.print(hasattr(res,'address'))
  14. 5.print(delattr(res,'job')) #删除job
  15. 6.print(hasattr(res,'job')) #不存在job对象,del和delattr都是删除对象!
  16. res.name = 'echo' #修改name属性
  17. 7.print(res.name)

输出结果:

  1. 1.IT
  2. 2.50
  3. 3.True
  4. 4.False
  5. 5.None
  6. 6.False
  7. 7.echo

类的专用方法总结:

  1. hasattr(res, 'age') # 如果存在 'age' 属性返回 True。
  2. getattr(res, 'age') # 返回 'age' 属性的值
  3. setattr(res, 'age', 8) # 添加属性 'age' 值为 8
  4. delattr(res, 'age') # 删除属性 'age'

内置类属性:

  1. __dict__ : 类的属性(包含一个字典,由类的数据属性组成)
  2. __doc__ :类的文档字符串
  3. __name__: 类名
  4. __module__: 类定义所在的模块(类的全名是'__main__.className',如果类位于一个导入模块mymod中,那么className.__module__ 等于 mymod
  5. __bases__ : 类的所有父类构成元素(包含了一个由所有父类组成的元组)

Python:面向对象的更多相关文章

  1. python 面向对象初级篇

    Python 面向对象(初级篇) 概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发" ...

  2. Python 面向对象 基础

    编程范式概述:面向过程 和 面向对象 以及函数式编程 面向过程:(Procedure Oriented)是一种以事件为中心的编程思想. 就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现 ...

  3. python面向对象进阶(八)

    上一篇<Python 面向对象初级(七)>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使 ...

  4. python 面向对象(进阶篇)

    上一篇<Python 面向对象(初级篇)>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使 ...

  5. python 面向对象编程学习

    1. 问题:将所有代码放入一个py文件:无法维护 方案:如果将代码才分放到多个py文件,好处: 1. 同一个名字的变量互相不影响 2.易于维护 3.引用模块: import module 2.包:解决 ...

  6. Python面向对象详解

    Python面向对象的"怜人之处" Python的待客之道--谁能进来 Python的封装--只给你想要的 Python的继承--到处认干爹 Python的多态--说是就是

  7. python 面向对象和类成员和异常处理

    python 面向对象 你把自己想象成一个上帝,你要创造一个星球,首先你要把它揉成一个个球,两个直径就能创造一个球 class star: '''名字(name),赤道直径(equatorial di ...

  8. python 面向对象学习

    ------Python面向对象初 下面写一个类的简单实用,以便方便理解类 #python 3.5环境,解释器在linux需要改变 #阅读手册查询readme文件 #作者:S12-陈金彭 class ...

  9. 初识python面向对象

    一.初识python面向对象: class Person: #使用class关键字定义一个类 age=0 #类变量(静态变量) def eat(self,food): #定义一个方法 self.age ...

  10. python 面向对象、特殊方法与多范式、对象的属性及与其他语言的差异

    1.python 面向对象 文章内容摘自:http://www.cnblogs.com/vamei/archive/2012/06/02/2532018.html   1.__init__() 创建对 ...

随机推荐

  1. jquery获得图片的真实大小

    $(function(){ var imgSrc = $("#image").attr("src"); getImageWidth(imgSrc,functio ...

  2. linux -小记(2)问题:yum 安装报错"Another app is currently holding the yum lock; waiting for it to exit... ...: yum Memory : 26 M RSS (868 MB VSZ) Started: Wed Oct 26 22:48:24 2016 - 0"

    yum 安装报错 "Another app is currently holding the yum lock; waiting for it to exit... The other ap ...

  3. php_sapi_name详解

    php_sapi_name : — 返回 web 服务器和 PHP 之间的接口类型. 返回描述 PHP 所使用的接口类型(the Server API, SAPI)的小写字符串. 例如,CLI 的 P ...

  4. CSAPP(前言)

    很久之前就听过有过CSAPP这本书的传闻了,今天终于决定上手这本神作:既然是神作,就要仔细拜读一下,今天看了一下前言部分还真的令人耳目一新,单单是前言部分就让我学习到几个新的知识点: 1.c和Java ...

  5. shell script练习

    执行脚本的几种方式: 1. sh a.sh 或者  bash a.sh  调用的是 /bin/bash 进程执行的,所以脚本不需要执行权限. 2. 直接使用绝对路径执行, /home/script/a ...

  6. CRM JS 设置lookup字段 setSimpleLookupValue

    function setSimpleLookupValue(LookupId, Type, Id, Name) { /// <summary> /// Sets the value for ...

  7. 全面了解 Linux 服务器 - 4. 查看 Linux 系统的平均负载

    可使用 uptime.top.w 命令来查看. 以 uptime 命令为例: liuqian@ubuntu:~$ uptime 17:31:26 up 7:27, 2 users, load aver ...

  8. eclipse中如何关闭运行时自动保存?

    Eclipse没有提供自动保存的功能,只能自己写脚本每隔多久保存一次,或监听修改即保存.设置方法:1.打开:preferences>run/debug>launching2.选择save ...

  9. C#中定义数组

    C#定义数组 一.一维:int[] numbers = new int[]{1,2,3,4,5,6}; //不定长 int[] numbers = new int[3]{1,2,3};//定长   二 ...

  10. Server 2003序列号

    windows2003 64位注册码 Windows 2003 R2 64bit Enterprise VOL Edition 企业版 MR78C-GF2CY-KC864-DTG74-VMT73 VP ...