面向对象编程概述
面向对象编程即OOP编程,区别于面向过程编程,面向对象编程比较适合开发大项目,会以更接近人类的思维的角度去写程序。
类和对象
类:具有某种特征的事物的集合(群体)。
对象:群体(类)里面的个体。
类是抽象的,对象是具体的。

image.png
构造函数
>>>b=cl2()
>>>c=cl3()
>>>c=cl3("xuehuai","student")

image.png

image.png

image.png

image.png
类在实例化的时候自动首先触发的方法
属性和方法
属性:静态的特征。如头发、手臂等。
方法:动态的特征。如唱歌、写字等。
>>>c=cl4("xuehuai","student")
>>>c.myname
>>>c.myjob
>>>c=cl5()
>>>c.myfunc1()
>>>c=cl5()
>>>c.myfunc1("xuehuai")
>>>c=cl6("xuehuai")
c.myfunc1()

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png
继承与重载
继承:把某一个或多个类(基类)的特征拿过来
重载:在子类(派生类)里面对继承过来的特征重新定义。
父类:基类
子类:派生类
>>>s=son()
>>>s.speak()
>>>d=daughter()
>>>d.speak()
>>>d.write()
>>>d.listen()
>>>s=son2()
>>>s.speak()

image.png

image.png
作业
将多个Excel表格里面的内容合并到一个文件中。
# -*- coding: utf-8 -*-
#将多个Excel文件合并成一个
import xlrd
import xlsxwriter
#打开一个excel文件
def open_xls(file):
fh=xlrd.open_workbook(file)
return fh
#获取excel中所有的sheet表
def getsheet(fh):
return fh.sheets()
#获取sheet表的行数
def getnrows(fh,sheet):
table=fh.sheets()[sheet]
return table.nrows
#读取文件内容并返回行内容
def getFilect(file,shnum):
fh=open_xls(file)
table=fh.sheets()[shnum]
num=table.nrows
for row in range(num):
rdata=table.row_values(row)
datavalue.append(rdata)
return datavalue
#获取sheet表的个数
def getshnum(fh):
x=0
sh=getsheet(fh)
for sheet in sh:
x+=1
return x
if __name__=='__main__':
#定义要合并的excel文件列表
allxls=['F:/DTLFolder/第一个测试文件.xlsx','F:/DTLFolder/第二个测试文件.xlsx','F:/DTLFolder/第三个测试文件.xlsx']
#存储所有读取的结果
datavalue=[]
for fl in allxls:
fh=open_xls(fl)
x=getshnum(fh)
for shnum in range(x):
print("正在读取文件:"+str(fl)+"的第"+str(shnum)+"个sheet表的内容...")
rvalue=getFilect(fl,shnum)
#定义最终合并后生成的新文件
endfile='F:/DTLFolder/excel3.xlsx'
wb1=xlsxwriter.Workbook(endfile)
#创建一个sheet工作对象
ws=wb1.add_worksheet()
for a in range(len(rvalue)):
for b in range(len(rvalue[a])):
c=rvalue[a][b]
ws.write(a,b,c)
wb1.close()
print("文件合并完成")

image.png
网友评论