190411Python面向对象编程
一、面向对象的概念
- 类:把一类事物的相同特征抽取出来整合到一起就是一个类,类是一个抽象的概念
- 对象:基于类创建的一个具体的事物
class People(object):
'这是一个人类的类'
def __init__(self,name,age,gender):
self.name = name
self.age = age
self.gender = gender
def say_hello(self): #函数属性
print("%s 您好!" % self.name)
def get_attr(self):
print("您今年已经 %s 岁了" % self.age)
def eat_food(self,food):
print("%s 正在吃 %s." % (self.name,food))
p1 = People("Dongfei",18,"M") #实例化,p1 = People.__init__(p1,"dongfei",18,"M")
p1.say_hello()
p1.get_attr()
p1.eat_food("饺子")
print(People.__dict__) #查看类的属性字典
print(People.__name__) #类名
print(People.__doc__) #描述
print(People.__base__)
print(People.__bases__)
print(People.__module__) #显示这个类所在哪个模块
print(People.__class__) #类型
二、类属性的增删查改
class People(object):
'这是一个人类的类'
addr = "earth"
def __init__(self,name,age,gender):
self.name = name
self.age = age
self.gender = gender
def say_hello(self): #函数属性
print("%s 您好!" % self.name)
def get_attr(self):
print("您今年已经 %s 岁了" % self.age)
def eat_food(self,food):
print("%s 正在吃 %s." % (self.name,food))
print(People.addr) #查,earth
People.addr = "Mars" #改
print(People.addr) #Mars
People.status = "alive" #增
print(People.status) #alive
del People.addr #删
三、实例属性的增删查改
class People(object):
'这是一个人类的类'
addr = "earth"
def __init__(self,name,age,gender):
self.name = name
self.age = age
self.gender = gender
def say_hello(self): #函数属性
print("%s 您好!" % self.name)
def get_attr(self):
print("您今年已经 %s 岁了" % self.age)
def eat_food(self,food):
print("%s 正在吃 %s." % (self.name,food))
p1 = People("Dongfei",18,"M")
print(p1.__dict__) #实例属性字典,{'gender': 'M', 'age': 18, 'name': 'Dongfei'}
print(p1.name) #查,Dongfei
print(p1.addr) #类的数据属性,earth
p1.hobby = "Python" #增
print(p1.__dict__) #{'name': 'Dongfei', 'hobby': 'Python', 'age': 18, 'gender': 'M'}
p1.age = 19 #改
print(p1.__dict__) #{'age': 19, 'name': 'Dongfei', 'hobby': 'Python', 'gender': 'M'}
del p1.gender
print(p1.__dict__) #{'name': 'Dongfei', 'age': 19, 'hobby': 'Python'}
四、静态属性,类方法,静态方法
- property
class Room():
x = 1
def __init__(self,name,owner,width,length,heigh):
self.name = name
self.owner = owner
self.width = width
self.length = length
self.heigh = heigh
@property #静态属性,可以封装逻辑
def cal_area(self):
# print("%s 的 %s 面积是 %s" %(self.owner,self.name,self.width*self.length))
return self.width*self.length
r1 = Room("loushi", "dongfei", 10, 10, 5)
print(r1.x) #1
print(r1.cal_area) #100
- classmethod
class Room():
x = 1
def __init__(self,name,owner,width,length,heigh):
self.name = name
self.owner = owner
self.width = width
self.length = length
self.heigh = heigh
@classmethod #类方法,直接用类来调用
def get_x(cls):
return cls.x
print(Room.get_x()) #1,调用类方法
- staticmethod
class Room():
x = 1
def __init__(self,name,owner,width,length,heigh):
self.name = name
self.owner = owner
self.width = width
self.length = length
self.heigh = heigh
@staticmethod #静态方法,类的工具包,不能使用类变量和实例变量
def Bedroom(name):
print("%s is sleepping ..." % name)
Room.Bedroom("dongfei") #类调用
r1 = Room("loushi", "dongfei", 10, 10, 5)
r1.Bedroom("dongfei") #实例调用
五、组合
- 示例
class Hand():
pass
class Body():
pass
class Foot():
pass
class Person():
def __init__(self,id,name):
self.Id = id
self.Name = name
self.Hand = Hand()
self.Body = Body()
self.Foot = Foot()
p1 = Person('000001', 'jack')
print(p1.__dict__) #{'Id': '000001', 'Name': 'jack', 'Foot': <__main__.Foot object at 0x000000000106B278>, 'Hand': <__main__.Hand object at 0x000000000106B208>, 'Body': <__main__.Body object at 0x000000000106B240>}
- 示例2
class School():
def __init__(self,name,addr):
self.name = name
self.addr = addr
class Course():
def __init__(self,name,price,school_name,addr):
self.name = name
self.price = price
self.school = School(school_name,addr)
c1 = Course("python",1000,"希望小学","beijing")
print(c1.school.name)
六、面向对象的三大特性
- 继承、接口继承
- 继承顺序:深度优先、广度优先,python3 都是新式类,遵循广度优先
class ParentClass1():
pass
class ParentClass2():
pass
class SubClass1(ParentClass1): #单继承
pass
class SubClass2(ParentClass1,ParentClass2): #多继承
pass
class ParentClass1():
x = 1
def __init__(self,name):
self.name = name
class ParentClass2():
y = 2
class SubClass1(ParentClass1): #单继承
def __init__(self,name,age):
self.name = name
self.age = age
class SubClass2(ParentClass1,ParentClass2): #多继承
pass
s1 = SubClass1("taiwen",20)
print(s1.x) #1
print(s1.name) #taiwen
print(s1.age) #20
import abc
class All_File(metaclass=abc.ABCMeta):
@abc.abstractmethod #接口继承,子类中必须要定义read方法
def read(self):
pass #接口类不需要写逻辑
@abc.abstractmethod
def write(self):
pass
class Disk(All_File):
def read(self):
print('disk read')
def write(self):
print('disk write')
d1 = Disk()
d1.write()
- 多态:不同的对象去调用相同的方法就是多态的体现
- 封装
class People:
_star1 = "earth1"
__star2 = "earth2" # 限制类的外部访问
def __init__(self,id,name,age):
self.id = id
self.name = name
self.age = age
p1 = People(1,"jack",20)
print(p1._star1) #earth1
print(p1._People__star2) #earth12
七、子类中调用父类的方法
class Vehicle:
Country = "China"
def __init__(self,name,speed,load,power):
self.name = name
self.speed = speed
self.load = load
self.power = power
def run(self):
print("runing")
class Subway(Vehicle):
def __init__(self,name,speed,load,power,line):
Vehicle.__init__(self,name,speed,load,power) #继承父类的数据属性
self.line = line
def show_info(self):
print(self.name, self.speed,self.power,self.line)
def run(self): #子类中调用父类的方法
Vehicle.run(self)
print("%s %s running" % (self.name,self.line))
line10 = Subway("地铁","50m/s",10000000,"Electricity",10)
line10.show_info()
line10.run()
- super()方法
class Vehicle:
Country = "China"
def __init__(self,name,speed,load,power):
self.name = name
self.speed = speed
self.load = load
self.power = power
def run(self):
print("runing")
class Subway(Vehicle):
def __init__(self,name,speed,load,power,line):
# Vehicle.__init__(self,name,speed,load,power) #继承父类的数据属性
super().__init__(name,speed,load,power) # == super(Subway, self).__init__(name,speed,load,power)
self.line = line
def show_info(self):
print(self.name, self.speed,self.power,self.line)
def run(self): #子类中调用父类的方法
# Vehicle.run(self)
super().run()
print("%s %s running" % (self.name,self.line))
line10 = Subway("地铁","50m/s",10000000,"Electricity",10)
line10.show_info()
line10.run()
八、反射
- 程序可以访问、检测和修改它本身状态或行为的一种能力
- hasattr
print(hasattr(b1,'name')) #检测b1实例中有没有name这个属性
- getattr
print(getattr(b1,'name')) #获取b1实例中name属性,没有则会报错
print(getattr(b1,'name123',"查无此属性")) #没有返回“查无此属性”
- setattr
setattr(b1,'sb',True) #给对象设置属性
print(getattr(b1,'sb'))
- delattr
delattr(b1,'addr')
print(getattr(b1,'addr','无此属性'))
九、动态导入模块
import importlib
m = importlib.import_module("m1.t1")
print(m)
m.t()
十、__getattr__、__delattr__、__setattr__ 在类中是用法
- 示例1
class T:
def __init__(self,x):
self.x = x
def __getattr__(self, item): #调用不存在的方法时会执行
print("in the getattr")
def __delattr__(self, item): #删除属性时会触发
self.__dict__.pop[item]
def __setattr__(self, key, value): #执行__init__时会触发
print("in the setattr")
#self.key = value #会进入递归
self.__dict__[key] = value
t1 = T(100)
res = getattr(t1,"nonono") #in the getattr
print(res) #None
delattr(t1,"x") #in the delattr
setattr(t1,"y",99)
print(t1.__dict__) #{'y': 99, 'x': 100}
- 示例2
class Foo:
def __init__(self,name):
self.name = name
def __getattr__(self, item):
print("[%s] 不存在" % item)
def __setattr__(self, key, value): #定制设置属性的过程
print("run setattr")
if type(value) is str:
print("开始设置")
self.__dict__[key] = value
else:
print("只支持字符串")
def __delattr__(self, item): #删除时触发
print("run delattr",item)
self.__dict__.pop(item)
f1 = Foo("dongfei")
f1.age = "18" #触发__setattr__
print(f1.__dict__)
del f1.name
print(f1.__dict__)
十一、自定义标准class
- 继承/派生
class List(list):
def show_medlle(self):
mid_index = int(len(self)/2)
return self[mid_index]
def append(self, p_object):
print("只能添加字符串类型")
if type(p_object) is str:
super().append(p_object)
l1 = List("helloworld")
print(l1)
print(l1.show_medlle())
l1.append("abc")
print(l1)
- 授权
import time
class FileHandl:
def __init__(self,filename,mode="r",encoding="utf-8"):
self.file = open(filename,mode,encoding=encoding)
def write(self,line):
date = time.strftime("%Y-%m-%d %X")
self.file.write("%s %s" % (date,line))
def __getattr__(self, item):
print(item)
return getattr(self.file,item)
f1 = FileHandl("a.txt", "r+")
# print(f1.read())
f1.write("testtxettestxt\n")
f1.write("testtxettestxt\n")
f1.seek(0)
print(f1.read())
十二、面向对象进阶
1、isinstance和issubclass
- isinstance
class Foo:
pass
f1 = Foo()
print(isinstance(f1,Foo)) #判断f1实例是否由Foo类实例化而来的
- issubclass
class Foo:
pass
class Bar(Foo):
pass
print(issubclass(Bar,Foo)) #Bar是否是Foo的子类
2、getattribute
class Foo:
def __init__(self,name):
self.name = name
def __getattr__(self, item): #当getattribute抛出AttributeError异常时执行
print("in the getattr")
def __getattribute__(self, item): #不管属性是否可以找到,都会触发getattribute
print("in the getattribute")
raise AttributeError("抛出异常")
f1 = Foo("hello")
f1.nonono #in the getattribute \n in the getattr
3、getitem、setitem和delitem
class Foo:
def __getitem__(self, item):
print("getitem")
return self.__dict__[item]
def __setitem__(self, key, value): #支持字典的方式设置值
print("setitem")
self.__dict__[key] = value
def __delitem__(self, key):
print("delitem")
self.__dict__.pop(key)
f1 = Foo()
print(f1.__dict__)
f1["name"] = "dongfei"
print(f1.__dict__)
del f1["name"]
print(f1.__dict__)
f1["name"] = "jack"
print(f1["name"])
4、str
class Foo():
def __init__(self,name,age):
self.name = name
self.age = age
def __str__(self): #控制实例的返回值
return "foo 的返回值:名字[%s] 年龄[%s]" %(self.name,self.age)
f1 = Foo("dongfei",18)
print(f1) #foo 的返回值:名字[dongfei] 年龄[18]
5、repr
class Foo():
def __init__(self,name,age):
self.name = name
self.age = age
def __repr__(self): #f1.__repr__() #应用在解释器中
return "foo 的返回值:名字[%s] 年龄[%s]" %(self.name,self.age)
f1 = Foo("dongfei",18)
6、format
format_dic = {
'ymd':'{0.year}:{0.mon}:{0.day}',
'mdy':'{0.mon}:{0.day}:{0.year}'
}
class Date:
def __init__(self,year,mon,day):
self.year = year
self.mon = mon
self.day = day
def __format__(self, format_spec):
print('run format')
print(format_spec)
if not format_spec or format_spec not in format_dic:
format_spec = 'ymd'
fm = format_dic[format_spec]
return fm.format(self)
d1 = Date(2019,4,19)
x = '{0.year} {0.mon} {0.day}'.format(d1)
y = '{0.year}:{0.mon}:{0.day}'.format(d1)
print(x)
print(y)
z = format(d1)
print(z)
7、slots
为了节省内存空间,可以用slots代替dict,实例只能定义slots已有的属性
class Foo:
__slots__ = "name"
f1 = Foo()
f1.name = "dongfei"
print(f1.name)
print(f1.__slots__)
8、doc
class Foo:
'类的描述信息'
pass
print(Foo.__doc__) #类的描述信息,该属性不能被继承
9、del
class Foo:
def __init__(self,name):
self.name = name
def __del__(self): #python解释器删除时触发
print("析构函数执行。。。")
f1 = Foo("jack")
del f1.name
10、call
class Foo:
def __call__(self, *args, **kwargs):
print("in the call")
f1 = Foo()
f1() #in the call
11、iter和next
class Foo:
def __init__(self,n):
self.n = n
def __iter__(self): #将对象变成可迭代的对象
return self
def __next__(self):
if self.n == 1000:
raise StopIteration("终止")
self.n += 1
return self.n
f1 = Foo(1)
for i in f1: #f1.__iter__()
print(i)
- 斐波那契数列
class Fib:
def __init__(self):
self._a = 1
self._b = 1
def __iter__(self):
return self
def __next__(self):
if self._a > 10000:
raise StopIteration("终止")
self._a, self._b = self._b, self._a + self._b
return self._a
f1 = Fib()
for i in f1:
print(i)
12、描述符get,set,delete (新式类的特性)
类属性>数据描述符>实例属性>非数据描述符(无set方法的描述符)
class Foo:
def __get__(self, instance, owner): #调用一个属性时触发
print("in the get")
def __set__(self, instance, value): #为一个属性赋值时触发
print("in the set")
def __delete__(self, instance): #用del删除属性时触发
print("in the delete")
class Bar:
x = Foo()
b1 = Bar()
b1.x #in the get
b1.x = 1 #in the set
del b1.x #in the delete
- 实例化前检查参数类型
class Typed:
def __init__(self,key,expected_type):
self.key = key
self.expected_type = expected_type
def __get__(self, instance, owner):
# print("run get")
# print('instance, owner',instance,owner)
return instance.__dict__[self.key]
def __set__(self, instance, value):
# print("run set")
# print('instance, value', instance, value)
if not isinstance(value,self.expected_type):
raise TypeError("type error")
instance.__dict__[self.key] = value
def __delete__(self, instance):
# print("run delete")
instance.__dict__.pop(self.key)
class People:
name = Typed("name",str)
age = Typed("age",int)
salary = Typed("salary",float)
def __init__(self,name,age,salary):
self.name = name
self.age = age
self.salary = salary
p1 = People("dongfei",18,1800)
# print(p1.name)
# del p1.age
# print(p1.__dict__)
print(p1.name,p1.age,p1.salary)
13、enter和exit
class Open:
def __init__(self,name):
self.name = name
def __enter__(self):
print("run enter")
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print("run exit")
with Open("a.txt") as f: #在此触发__enter__,enter返回值赋值给f
print("xxx")
#代码结束时触发__exit__
class Open:
def __init__(self,name):
self.name = name
def __enter__(self):
print("run enter")
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print("run exit")
print(exc_type) #异常类
print(exc_val) #异常的值
print(exc_tb) #异常的异常信息
return True
with Open("a.txt") as f: #在此触发__enter__,enter返回值赋值给f
print(f)
print(nono)
print("xxx")
#代码结束时触发__exit__
#输出:
# run enter
# <__main__.Open object at 0x0000000001084160>
# run exit
# <class 'NameError'>
# name 'nono' is not defined
# <traceback object at 0x000000000108A288>
十三、类的装饰器
def Typed(**kwargs):
def deco(obj):
print(kwargs)
print(obj)
for key,val in kwargs.items():
setattr(obj,key,val)
return obj
return deco
@Typed(x=1,y=2)
class Foo:
pass
print(Foo.__dict__)
@Typed(name="dongfei")
class Bar:
pass
print(Bar.__dict__)
- 描述符和装饰器的应用示例
class Typed:
def __init__(self,key,expected_type):
self.key = key
self.expected_type = expected_type
def __get__(self, instance, owner):
# print("run get")
# print('instance, owner',instance,owner)
return instance.__dict__[self.key]
def __set__(self, instance, value):
# print("run set")
# print('instance, value', instance, value)
if not isinstance(value,self.expected_type):
raise TypeError("type error")
instance.__dict__[self.key] = value
def __delete__(self, instance):
# print("run delete")
instance.__dict__.pop(self.key)
def deco(**kwargs):
def wrapper(obj):
# print(kwargs)
# print(obj)
for key,val in kwargs.items():
setattr(obj,key,Typed(key,val))
return obj
return wrapper
@deco(name=str,age=int,salary=float)
class People:
# name = Typed("name",str)
# age = Typed("age",int)
# salary = Typed("salary",float)
def __init__(self,name,age,salary):
self.name = name
self.age = age
self.salary = salary
p1 = People("dongfei",18,1800.1)
# print(p1.name)
# del p1.age
# print(p1.__dict__)
print(p1.name,p1.age,p1.salary)
print(People.__dict__)
十四、利用描述符和装饰器自定制property
class Lazyproperty:
def __init__(self,func):
self.func = func
def __get__(self, instance, owner):
print("run get")
if instance is None:
return self
res = self.func(instance)
setattr(instance,self.func.__name__,res) #缓存功能
return res
# def __set__(self, instance, value):
# pass
class Room:
def __init__(self,name,width,length):
self.name = name
self.width = width
self.length = length
@Lazyproperty #area = Lazproperty(area)
def area(self):
return self.width * self.length
r1 = Room("卧室",3,4)
print(r1.area)
print(Room.area)
print(r1.__dict__)
print(r1.area)
190411Python面向对象编程的更多相关文章
- angular2系列教程(六)两种pipe:函数式编程与面向对象编程
今天,我们要讲的是angualr2的pipe这个知识点. 例子
- 带你一分钟理解闭包--js面向对象编程
上一篇<简单粗暴地理解js原型链--js面向对象编程>没想到能攒到这么多赞,实属意外.分享是个好事情,尤其是分享自己的学习感悟.所以网上关于原型链.闭包.作用域等文章多如牛毛,很多文章写得 ...
- PHP 面向对象编程和设计模式 (1/5) - 抽象类、对象接口、instanceof 和契约式编程
PHP高级程序设计 学习笔记 2014.06.09 什么是面向对象编程 面向对象编程(Object Oriented Programming,OOP)是一种计算机编程架构.OOP 的一条基本原则是计算 ...
- Delphi_09_Delphi_Object_Pascal_面向对象编程
今天这里讨论一下Delphi中的面向对象编程,这里不做过多过细的讨论,主要做提纲挈领的描述,帮助自己抓做重点. 本随笔分为两部分: 一.面向对象编程 二.面向对象编程详细描述 ------------ ...
- python基础-面向对象编程
一.三大编程范式 编程范式即编程的方法论,标识一种编程风格 三大编程范式: 1.面向过程编程 2.函数式编程 3.面向对象编程 二.编程进化论 1.编程最开始就是无组织无结构,从简单控制流中按步写指令 ...
- 面向对象编程(OOP)
什么是面向对象编程,对于面向对象编程与面向过程编程的解释随处可见,个人认为对面向对象编程解释最好的一个定义是:依赖倒转原则是面向对象编程的标志,面向对象编程是一种思想,无论使用哪一种编程语言,如果在编 ...
- python 学习笔记7 面向对象编程
一.概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发"更快更好更强..." ...
- 进击的Python【第七章】:Python的高级应用(四)面向对象编程进阶
Python的高级应用(三)面向对象编程进阶 本章学习要点: 面向对象高级语法部分 静态方法.类方法.属性方法 类的特殊方法 反射 异常处理 Socket开发基础 一.面向对象高级语法部分 静态方法 ...
- 进击的Python【第六章】:Python的高级应用(三)面向对象编程
Python的高级应用(三)面向对象编程 本章学习要点: 面向对象编程介绍 面向对象与面向过程编程的区别 为什么要用面向对象编程思想 面向对象的相关概念 一.面向对象编程介绍 面向对象程序设计(英语: ...
随机推荐
- 【技术调研】最强Node-RED初探总结
在某个项目中需要调研下node-red的功能,我大概花了三天时间研究了相关的官方文档,写了几个Demo总结了下node-red相关的功能.如需转载,请注明出处 https://www.cnblogs. ...
- DAY.15_Python
昨天完成了三级菜单和购物车程序的作业: """ .__author__.=,"JerseyHg" 作业要求:1. 可依次选择进入到下一级菜单:2. 可 ...
- http头部信息解析
HTTP 头部解释 1. Accept:告诉WEB服务器自己接受什么介质类型,*/* 表示任何类型,type/* 表示该类型下的所有子类型,type/sub-type. 2. Accept-Chars ...
- 2014蓝桥杯B组初赛试题《六角填数》
题目描述: 如图[1.png]所示六角形中,填入1~12的数字. 使得每条直线上的数字之和都相同. 图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少? 请通过浏览器提交 ...
- swift基本运算
swift运算有单目运算,双目运算和三元运算 1:赋值操作 iX = iY//iX is 8 元组赋值 let (iX, iY = (, ) // iX is 8, iY is 7 和c语言不同的是, ...
- Luogu 4781 【模板】拉格朗日插值
模板题. 拉格朗日插值的精髓在于这个公式 $$f(x) = \sum_{i = 1}^{n}y_i\prod _{j \neq i}\frac{x - x_i}{x_j - x_i}$$ 其中$(x_ ...
- 第十九课 pluginlib&Nodelet
把rgb摄像头的数据转换为laser的时候使用了Nodelet. pluginlib(插件库) 在ros中有一个plugin的包,下面是一个ROS Plugin Registration的例子 上面包 ...
- 6.python探测Web服务质量方法之pycurl模块
才开始学习的时候有点忽略了这个模块,觉得既然Python3提供了requests库,为什么多此一举学习这个模块.后来才发现pycurl在探测Web服务器的时候的强大. pycurl是一个用c语言写的l ...
- html页面源代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 《深度学习原理与TensorFlow实践》喻俨,莫瑜
1. 深度学习简介 2. TensorFlow系统介绍 3. Hello TensorFlow 4. CNN看懂世界 5. RNN能说会道 6. CNN LSTM看图说话 7. 损失函数与优化算法 T ...