(python)面向对象
函数式编程和面向对象对比
a. round 1 请开发一个消息提醒的功能(邮件/短信/微信)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def email(em,text): """ 发送邮件 :return: """ print(em,text) def msg(tel,text): """ 发送短信 :return: """ print(tel,text) def wechat(num,text): """ 发送微信 :return: """ print(num,text) # 编写功能:假设用户购买课程,然后给alex发送提醒; if 1==1: msg('188888888','张进购买了一个学位课') email('alex@sb.com','张进购买了一个学位课') wechat('xxxx','张进购买了一个学位课')
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Message: def email(self, em, text): """ 发送邮件 :return: """ print(em,text) def msg(self, tel, text): """ 发送短信 :return: """ print(tel,text) def wechat(self, num, text): """ 发送微信 :return: """ print(num,text) # 编写功能:假设用户购买课程,然后给alex发送提醒; if 1==1: obj = Message() obj.email('alex@sb.com', '张进购买了一个学位课') obj.msg('188888888','张进购买了一个学位课') obj.wechat('xxxx','张进购买了一个学位课')
初步对比:
函数:定义简单/调用简单
面向对象:定义复杂/调用复杂/ 好处:归类,将某些类似的函数写在一起
总结:
1.函数式编程可能会比面向对象好.
2.python中支持的两种编程方式
3.面向对象方式格式
定义: class 类名 定义了一个类
def 函数名(self) 在类中编写了一个"方法"
pass
调用:
x1 = 类名() 创建了一个对象/实例化了一个对象
x1.函数名() 通过对象调用其中一个方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Account: def login(self): user = input('请输入用户名:') pwd = input('请输入密码:') if user == 'alex' and pwd == 'sb': print('登录成功') else: print('登录失败') obj = Account() obj.login()
a. round 2 打印
"""
完成以下功能:
老狗/20岁/男/上山去砍柴
老狗/20岁/男/开车去东北
老狗/20岁/男/喜欢大宝剑
"""
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def kc(name,age,gender): data = "%s,性别%s,今年%s岁,喜欢上山砍柴" %(name,gender,age) print(data) def db(name,age,gender): data = "%s,性别%s,今年%s岁,喜欢开车去东北" %(name,gender,age) print(data) def bj(name,age,gender): data = "%s,性别%s,今年%s岁,喜欢大宝剑" %(name,gender,age) print(data) kc('老狗',20,'男') kc('老狗',20,'男') db('老狗',20,'男') bj('老狗',20,'男')
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class LaoGou: def __init__(self,name,age,gender): # 特殊的方法,如果 类名() ,则该方法会被自动执行 (构造方法) self.n1 = name self.n2 = age self.n3 = gender def kc(self): data = "%s,性别%s,今年%s岁,喜欢上山砍柴" %(self.n1,self.n3,self.n2) print(data) def db(self): data = "%s,性别%s,今年%s岁,喜欢开车去东北" %(self.n1,self.n3,self.n2) print(data) def bj(self): data = "%s,性别%s,今年%s岁,喜欢大宝剑" %(self.n1,self.n3,self.n2) print(data) obj = LaoGou('老狗',20,'男') obj.kc() obj.db() obj.bj()
总结:
1.构造方法
实例一:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Foo: def __init__(self,name): 构造方法,目的进行数据初始化. self.name = name self.age = 18 obj = Foo('侯明魏')
通过构造方法,可以将数据进行打包,以后使用时,去其中获取即可.
2.应用
a.将数据封装到对象中,以供自己在方法中调用
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class FileHandler: def __init__(self,file_path): self.file_path = file_path self.f = open(self.file_path, 'rb') def read_first(self): # self.f.read() # ... pass def read_last(self): # self.f.read() # ... pass def read_second(self): # self.f... # ... pass obj = FileHandler('C:/xx/xx.log') obj.read_first() obj.read_last() obj.read_second() obj.f.close()
b.将数据封装到对象中,以供其他函数调用
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 def new_func(arg): 2 arg.k1 3 arg.k2 4 arg.k6 5 6 class Foo: 7 def __init__(self,k1,k2,k6): 8 self.k1 = k1 9 self.k2 = k2 10 self.k6 = k6 11 12 obj = Foo(111,22,333) 13 new_func(obj)
练习:
1:用户登录
2:显示当前的用户信息
3:查看当前用户所有账单
4:购买姑娘形状的抱枕
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class UserInfo: 2 3 def __init__(self): 4 self.name = None 5 6 def info(self): 7 print('当前用户名称:%s' %(self.name,)) 8 9 def account(self): 10 print('当前用户%s的账单是:....' %(self.name,)) 11 12 def shopping(self): 13 print('%s购买了一个人形抱枕' %(self.name,)) 14 15 def login(self): 16 user = input('请输入用户名:') 17 pwd = input('请输入密码:') 18 if pwd == 'sb': 19 self.name = user 20 while True: 21 print(""" 22 1. 查看用户信息 23 2. 查看用户账单 24 3. 购买抱枕 25 """) 26 num = int(input('请输入选择的序号:')) 27 if num == 1: 28 self.info() 29 elif num ==2: 30 self.account() 31 elif num == 3: 32 self.shopping() 33 else: 34 print('序号不存在,请重新输入') 35 else: 36 print('登录失败') 37 38 obj = UserInfo() 39 obj.login()
面向对象的代码如何编写
a.规则
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Foo: 2 3 def __init__(self,name): 4 self.name = name 5 6 7 def detail(self,msg): 8 print(self.name,msg) 9 10 obj = Foo() 11 obj.detail()
b.什么时候写,如何写?
方法一:归类+提取公共值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class File: 2 def file_read(self,file_path): 3 pass 4 5 def file_update(self,file_path): 6 pass 7 8 def file_delete(self,file_path): 9 pass 10 11 def file_add(self,file_path): 12 pass 13 14 class Excel: 15 def excel_read(self,file_path): 16 pass 17 18 def excel_update(self,file_path): 19 pass 20 21 def excel_delete(self,file_path): 22 pass 23 24 def excel_add(self,file_path): 25 pass
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class File: 2 def __init__(self,file_path): 3 self.file_path = file_path 4 5 def file_read(self): 6 pass 7 8 def file_update(self): 9 pass 10 11 def file_delete(self): 12 pass 13 14 def file_add(self): 15 pass 16 17 class Excel: 18 def __init__(self,file_path): 19 self.file_path = file_path 20 21 def excel_read(self): 22 pass 23 24 def excel_update(self): 25 pass 26 27 def excel_delete(self): 28 pass 29 30 def excel_add(self): 31 pass 32
方法二:在指定类中编写和当前类的所有相关代码 + 提取公共值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Message: 2 def email(self): 3 pass 4 5 class Person: 6 def __init__(self,na, gen, age, fig) 7 self.name = na 8 self.gender = gen 9 self.age = age 10 self.fight =fig 11 12 def grassland(self): 13 self.fight = self.fight - 10 14 15 def practice(self): 16 self.fight = self.fight + 90 17 18 def incest(self): 19 self.fight = self.fight - 666 20 21 22 cang = Person('苍井井', '女', 18, 1000) # 创建苍井井角色 23 dong = Person('东尼木木', '男', 20, 1800) # 创建东尼木木角色 24 bo = Person('波多多', '女', 19, 2500) # 创建波多多角色 25 26 dong.grassland()
面向对象的三大特性:封装/继承/多态
封装:
将相关的功能封装到一个类中:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Message: def emil(self):pass def msg(self):pass def wechat(self):pass
将数据封装到一个对象中:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Message: def emil(self):pass def msg(self):pass def wechat(self):pass
继承:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class SuperBase: def f3(self): print('f3') class Base(SuperBase): # 父类,基类 def f2(self): print('f2') class Foo(Base): # 子类,派生类 def f1(self): print('f1') obj = Foo() obj.f1() obj.f2() obj.f3() # 原则:现在自己类中找,么有就去父类
总结:
1. 继承编写
class Foo(父类):
pass
2. 支持多继承(先找左/再找右)
3. 为什么要有多继承? 提供代码重用性
多态:
多种形态或者多种状态
由于python原声支持多态,所以没有特殊性
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Foo1: 2 def f1(self): 3 pass 4 5 class Foo2: 6 def f1(self): 7 pass 8 9 class Foo3: 10 def f1(self): 11 pass 12 13 14 def func(arg): 15 arg.f1() 16 17 obj = Foo1() # obj= Foo2() obj = Foo3() 18 func(obj)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Son(list): pass class Son1(list): pass # 以后传参时,arg可以是:list类的对象/list任何子类的对象 public void func(list arg){ print(arg) } # obj = list() # obj = Son() obj = Son1() func(obj)