一、面向对象的概念

  • 类:把一类事物的相同特征抽取出来整合到一起就是一个类,类是一个抽象的概念
  • 对象:基于类创建的一个具体的事物
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)

六、面向对象的三大特性

  • 继承、接口继承
  1. 继承顺序:深度优先、广度优先,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()

八、反射

  1. 程序可以访问、检测和修改它本身状态或行为的一种能力
  • 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面向对象编程的更多相关文章

  1. angular2系列教程(六)两种pipe:函数式编程与面向对象编程

    今天,我们要讲的是angualr2的pipe这个知识点. 例子

  2. 带你一分钟理解闭包--js面向对象编程

    上一篇<简单粗暴地理解js原型链--js面向对象编程>没想到能攒到这么多赞,实属意外.分享是个好事情,尤其是分享自己的学习感悟.所以网上关于原型链.闭包.作用域等文章多如牛毛,很多文章写得 ...

  3. PHP 面向对象编程和设计模式 (1/5) - 抽象类、对象接口、instanceof 和契约式编程

    PHP高级程序设计 学习笔记 2014.06.09 什么是面向对象编程 面向对象编程(Object Oriented Programming,OOP)是一种计算机编程架构.OOP 的一条基本原则是计算 ...

  4. Delphi_09_Delphi_Object_Pascal_面向对象编程

    今天这里讨论一下Delphi中的面向对象编程,这里不做过多过细的讨论,主要做提纲挈领的描述,帮助自己抓做重点. 本随笔分为两部分: 一.面向对象编程 二.面向对象编程详细描述 ------------ ...

  5. python基础-面向对象编程

    一.三大编程范式 编程范式即编程的方法论,标识一种编程风格 三大编程范式: 1.面向过程编程 2.函数式编程 3.面向对象编程 二.编程进化论 1.编程最开始就是无组织无结构,从简单控制流中按步写指令 ...

  6. 面向对象编程(OOP)

    什么是面向对象编程,对于面向对象编程与面向过程编程的解释随处可见,个人认为对面向对象编程解释最好的一个定义是:依赖倒转原则是面向对象编程的标志,面向对象编程是一种思想,无论使用哪一种编程语言,如果在编 ...

  7. python 学习笔记7 面向对象编程

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

  8. 进击的Python【第七章】:Python的高级应用(四)面向对象编程进阶

    Python的高级应用(三)面向对象编程进阶 本章学习要点: 面向对象高级语法部分 静态方法.类方法.属性方法 类的特殊方法 反射 异常处理 Socket开发基础 一.面向对象高级语法部分 静态方法 ...

  9. 进击的Python【第六章】:Python的高级应用(三)面向对象编程

    Python的高级应用(三)面向对象编程 本章学习要点: 面向对象编程介绍 面向对象与面向过程编程的区别 为什么要用面向对象编程思想 面向对象的相关概念 一.面向对象编程介绍 面向对象程序设计(英语: ...

随机推荐

  1. 【知识结构】最强Thymeleaf知识体系

    在开发一个小项目的时候,使用的是Spring Boot,Spring Boot 官方推荐的前端模板是thymeleaf, 花了两天时间将官方的文档看完并总结了下知识体系结构.转载请注明出处,https ...

  2. 配置环境是程序员的第一步 -- Xshell 6 免费版下载安装

    Xshell 是一个强大的安全终端模拟软件,通常用来连接云主机,远程控制云主机. 很多人都不知道 Xshell 有专门为家庭和学校用户提供的免费版,只需要填个用户名和邮箱即可. 免费版链接:https ...

  3. CrackMe的简单破解

    [CrackMe的简单破解] 对于以下这样的输入账号和密码的窗口,我们可以猜测该程序使用最简单的机制实现,即用strmp来比较用户输入的密码和原始密码匹配.所以为了破解该程序,可以通过bp strmp ...

  4. solrcloud学习记录

    1.今天看了下solrcloud提供的软提交的作用 硬提交:solr的建索引数据是要在提交时写入磁盘的,这是确保即便是停电也不会丢失数据: 软提交:为了提供更实时的检索能力,Solr设定了一种软提交方 ...

  5. 2014蓝桥杯B组初赛试题《切面条》

    题目描述: 一根高筋拉面,中间切一刀,可以得到2根面条.     如果先对折1次,中间切一刀,可以得到3根面条.     如果连续对折2次,中间切一刀,可以得到5根面条.     那么,连续对折10次 ...

  6. Capturing ASP.NET Application Startup Exceptions

    It has become common practice to perform tasks during an ASP.NET applications start up process. Thes ...

  7. MySQL性能调优与架构设计——第3章 MySQL存储引擎简介

    第3章 MySQL存储引擎简介 3.1 MySQL 存储引擎概述 MyISAM存储引擎是MySQL默认的存储引擎,也是目前MySQL使用最为广泛的存储引擎之一.他的前身就是我们在MySQL发展历程中所 ...

  8. cmake的一些词的解释

    cmake中一些预定义变量 PROJECT_SOURCE_DIR  工程的根目录 PROJECT_BINARY_DIR    运行cmake命令的目录,通常是${PROJECT_SOURCE_DIR} ...

  9. Redis 占用Windows系统盘空间23G

    Redis常出现问题总结: 1.当出现修改--maxheap and --heapdir 在启动这两个版本时都会创建一个 RedisQFork.dat文件,我不确定 RedisQFork 文件是否变小 ...

  10. 去掉html标签方法

    public static string CleanHtml(string strHtml) { strHtml = Regex.Replace(strHtml, @"(\<scrip ...