面向对象
分类
- 面像过程编程:初学者容易接受,从上往下依次执行。
- 面向函数编程:将某功能的代码封装为一个函数,使用时仅调用函数。
- 面向对象编程:对函数进行分类和封装
class people: # 经典类
class People(object): # 新式类,object类是所有类的基类/父类
def __init__(self,name,age): # 构造函数,当实例化对象时自动调用,self是实例化出来的对象
self.name = name # 属性
self.age = age
def run(self): # 方法
print "running...."
def __del__(self):
print "deleteing......" # 析构函数,删除实例化对象时自动调用;
class Student(People): # Student是子类,继承People这个父类;
pass
p1 = People("张策",18)
p1.run()
什么是面向对象?
类 ===== 建房子的图纸 (三室一厅,两室一厅…….)
对象===== 实际建出来的房子(门牌号)
class ThreeRoom:
pass
seven_zero_one = ThreeRoom()
seven_zero_one.live()
seven_zero_one.clean()
面向对象的三个特性:
封装
- 封装:把内容统一放在一个地方,看成一个整体,(实例化对象self和类的属性绑定在一起);
- 访问封装内容的两种方式:
通过self去访问封装的内容;(self.name)
通过实例化的对象名去访问封装的内容;(p1 = People(“westos”,17) p1.age)
继承(子承父业)
- 新名词:基类/派生类, 父类/子类, 新式类和经典类
- 多继承:
新式类: 广度优先继承;(python2.x和python3.x均支持)
经典类:深度优先继承;(python2.x支持,python3.x没有经典类) - 注意: 类的方法中可以传递一个对象;
class People(object): # 新式类,object类是所有类的基类/父类
def __init__(self,name,age): # 构造函数,当实例化对象时自动调用;
self.name = name # 属性
self.age = age
print "%s is create...." %(self.name)
def run(self): # 方法
print "%s running...." %(self.name)
def __del__(self):
print "deleteing......" # 析构函数,删除实例化对象时自动调用;
class Relation(object):
**def make_relation(self,obj):** #传入一个对象作为参数
print "%s is related with %s" %(self.name,obj.name)
class Student(People,Relation): # Student是子类,继承People这个父类;
def __init__(self,name, age, sid):
# People.__init__(self,name,age) # 如果父类名更改,此处也需要更改;
super(Student, self).__init__(name,age) # 更推荐使用
self.sid = sid
class Teacher(People,Relation):
def __init__(self, name, age, tid):
#People.__init__(self,name,age)
super(Teacher, self).__init__(name, age)
self.tid = tid
s1 = Student("lijian", 18, "007")
t1 = Teacher("westos", 18, "001")
s1.make_relation(t1)
多态
- 类的属性
在内存中只需要存储一次
在构造函数中的属性,每实例化一个就要存储一次
class People(object):
def __init__(self,name,country="China"):
self.name = name
self.country = country
p1 = People("飞天")
print p1.name
p2 = People("lala")
print p2.name
#结果为:
飞天
lala
- 多态
如果子类调用的方法,子类没有,父类有,运行父类;
如果子类调用的方法,子类有,父类也有,只运行子类的;
面向对象进阶
- 类变量,全局变量在内存中只存储一份
- 普通的对象属性,每个对象中都要存储一份
class People(object):
# def __init__(self,name,age,country="china"): #每创建一个实例就会在内存中重新存储一次
# self.name=name
# self.age=age
# self.country=country
country="China" #全局变量,类的属性,只需要存储一次
def __init__(self,name,age):
self.name=name
self.age=age
def run(self):
print "%s is running..."%self.name
p1=People("mj",18)
print p1.name ,p1.age,p1.country
print id(People.country),id(p1.country)
特殊属性
装饰器property
他就是装饰函数的一个函数
#该方法实现了装饰器的功能,但是调用函数的方式发生了改变。
import time
def dtimer(fun):
def timer(*args,**kwargs): # 高阶函数 #args = (1,2,3,4)
start_time = time.time()
fun(*args,**kwargs) # args =(1,2,3,4), 解包*(1,2,3,4)
stop_time = time.time()
return "run %s" % (stop_time - start_time)
return timer # 返回的是timer的地址,要执行该函数需要timer()
@dtimer # fun1 = dtimer(fun1)
def fun1(*args,**kwargs):
print "in the fun1....."
print args
time.sleep(1)
def fun2():
print "in the fun2....."
time.sleep(0.5)
print fun1(1,2,3,4)
# print timer(fun2)
#类中的几种方法
class Date(object):
def __init__(self,year, month,day):
self.year = year
self.month = month
self.day = day
def echo_date(self):
print "Year:",self.year
print "Month:",self.month
print "Day:",self.day
@classmethod
def from_str(cls,s): # class,类方法的第一个参数是类本身,cls = Date
year, month, day = map(int, s.split("-"))
d = cls(year,month,day) # d = Date(year,month,day)
return d # 对象
@staticmethod
def is_date_legal(s): # 静态方法,第一个参数既不是对象,也不是类本身,
year, month, day = map(int, s.split("-"))
return year >= 1970 and 0 < month <= 12 and 0 < day <= 31
#d = Date(2017,9,9)
#d.echo_date()
#d1 = Date.from_str('2018-10-19')
#d1.echo_date()
print "legal" if Date.is_date_legal('2017-13-16') else "illegal"
匿名函数
- lambda 形参 : 返回值
#这里的形参,可以赋默认值,形参和返回值都可以使用*args和**kwargs。
f = lambda x,y:x-y
print f (1,2)
#返回结果为1