面向对象之封装、继承
一、面向对象
1、创建一个类的基本格式,并调用
1 class 类名: 2 def __init__(self): # 必写 3 "数据的封装" 4 def 方法名(self): 5 "方法体" 6 7 # 调用 8 对象名=类名(参数1,参数2....) 9 对象名.方法名()
2、构造函数
类里的__init__方法属于构造函数,其作用是进行数据初始化
应用:
(1)将数据分装到对象中,供自己方法调用
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()
(2)、将数据封装到对象中,以供其他函数调用
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)
3、面向对象代码的编写
方式一:归类+提取公共值
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
提取公共值
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
# 将相同的参数封装在构造函数里,每个方法不需要传参
方法二:在指定的类中编写和当前类相关的所有代码+提取公共值
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()
二、面向对象的三大特性:分装、继承、多态
1、封装
1 # 将相关功能封装到一个类中: 2 class Message: 3 def email(self):pass 4 def msg(self):pass 5 def wechat(self):pass
1 # 将数据分装到一个对象中: 2 class Person: 3 def __init__(self,name,age,gender): 4 self.name = name 5 self.age = age 6 self.gender = gender 7 8 obj = Person('李静',18,'女')
2、继承,提高代码的重用性
一个子类可以继承多个父类,当调用的方法在子类里没有时,到父类的方法找,如果有则调用
1 class SuperBase: 2 def f3(self): 3 print('f3') 4 5 class Base(SuperBase): # 父类,基类 6 def f2(self): 7 print('f2') 8 9 class Foo(Base): # 子类,派生类 10 11 def f1(self): 12 print('f1') 13 14 obj = Foo() 15 obj.f1() 16 obj.f2() 17 obj.f3()
关于继承的查找顺序
练习1
1 class Base1: 2 def f1(self): 3 print("base1.1") 4 def f2(self): 5 print("base1.f2") 6 def f3(self): 7 print("base1.f3") 8 class Base2: 9 def f1(self): 10 print("base2.f1") 11 class Foo(Base1,Base2): 12 def f0(self): 13 print("f00.f0") 14 self.f3()
练习2
1 class Base1: 2 def f1(self): 3 print("base1.1") 4 def f2(self): 5 print("base1.f2") 6 def f3(self): 7 print("base1.f3") 8 class Base2: 9 def f1(self): 10 print("base2.f1") 11 class Foo(Base1,Base2): 12 def f0(self): 13 print("f00.f0") 14 self.f3() 15 16 obj=Foo() 17 obj.f0()