1.面向对象介绍

类和对象:是面向对象中两个重要概念 类:是对象对事物的抽象,比如人类\球类 对象:是类的一个实例,比如足球\篮球实例说明: 球类可以对球的特征和行为进行抽象,然后可以实例化一个真实的球体出来

为什么面向对象?

面向对象的主要思想是

封装

继承

多态

这种思想方面解决较为复杂的项目,而且维护起来较为容易

Python类定义

类定义: 类把需要的变量和函数组合成一起,这种包含称为"封装", class A(object):类的结构: class 类名 成员变量-属性 成员函数-方法

类的创建

class MyClass(object): def fun(self): print ("i am function")类的方法中至少有一个参数self

类脚本举例:

class People(object): color = 'yellow' def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") ren = People() print ren.color ren.think()2.类的属性

成员变量
对象的创建
创建对象的过程称之为实例化,当一个对象被创建后,包含三个方面的特性对象聚丙属性和方法,
句柄用于区分不同的对象,
对象的属性和方法,与类中的成员变量和成员函数对应,
obj = MyClass()创建类的一个实例,扩号对象,通过对象来调用方法和属性
类的属性
类的属性按使用范围分为公有属性和私有属性类的属性范围,取决于属性的名称,
共有属性---在内中和内外都能够调用的属性
私有属性---不能在内外贝类以外函数调用
定义方式:以""双下划线开始的成员变量就是私有属性
可以通过instance.classnameattribute方式访问,
内置属性--由系统在定义类的时候默认添加的由前后双下划线构成,如dic,module__

#!/usr/bin/env python#-*- coding:utf-8 -*-class People(object): color = 'yellow' __age = 30 #私有属性 def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__ageren = People()ren.color = '白色人'print ren.colorren.think()print '#'*30print("People.color")print ren.__People__age ##测试时使用。如要调用 时,通过方法内调用 。3.类的方法

成员函数

类的方法

方法的定义和函数一样,但是需要self作为第一个参数.

类方法为:

公有方法私有方法类方法静态方法

公有方法:在类中和类外都都测调用的方法.
私有方法:不测被类的外部调用模块,在方法前加个“__”c双下划线就是私有方法。

self参数:

用于区分函数和类的方法(必须有一个self)self参数表示执行对象本身

#!/usr/bin/env python#-*- coding:utf-8 -*-class People(object): color = 'yellow' __age = 30 #私有属性 def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def test(self):self.think() # 内部调用jack = People()jack.test() #外部调用

#!/usr/bin/env python#-*- coding:utf-8 -*-class People(object): color = 'yellow' __age = 30 #私有属性 def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__agedef __talk(self):print "I am talking with Tom" def test(self):self.__talk() # 内部调用talk()jack = People()jack.test() #外部调用类方法

类方法:被classmethod()函数处理过的函数,能被类所调用,也能被对象所调用(是继承的关系)。

静态方法:相当于“全局函数”,可以被类直接调用,可以被所有实例化对象共享,通过staticmethod()定义静态方法, 静态方法没有self参数

装饰器:

@classmethod()@staticmethod()

#!/usr/bin/env python#-*- coding:utf-8 -*-class People(object): color = 'yellow' __age = 30 #私有属性 def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__agedef __talk(self):print "I am talking with Tom" def test(self):print 'Testing....' cm = classmethod(test)jack = People()People.cm()

通过类方法类内的方法 ,不涉及的属性和方法 不会被加载,节省内存,快。

#!/usr/bin/env python#-*- coding:utf-8 -*-class People(object): color = 'yellow' __age = 30 #私有属性 def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__agedef __talk(self):print "I am talking with Tom" def test(): ##没有self 静态调用 会把所有的属性加载到内存里。print People.__age # 通过类访问内部变量 sm = staticmethod(test)jack = People()People.sm()

装饰调用类的方法:

#!/usr/bin/env python#-*- coding:utf-8 -*-class People(object): color = 'yellow' __age = 30 #私有属性 def think(self): self.color = "black" print "I am a %s " % self.color print ("I am a thinker") print self.__age def __talk(self): print "I am talking with Tom" @classmethod #调用类的方法 def test(self): print ("this is class method") @staticmethod #调用类的方法 def test1(): print ("this is static method")jack = People()People.test()People.test1()