上海交通大学-python程序设计课程PPT-Ch7-(1)...ppt_第1页
上海交通大学-python程序设计课程PPT-Ch7-(1)...ppt_第2页
上海交通大学-python程序设计课程PPT-Ch7-(1)...ppt_第3页
上海交通大学-python程序设计课程PPT-Ch7-(1)...ppt_第4页
上海交通大学-python程序设计课程PPT-Ch7-(1)...ppt_第5页
免费预览已结束,剩余63页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、面向对象思想和编程,2,2,2,回顾,程序包括 数据 数据类型,数据结构 处理过程 算法 两种程序设计思想 面向过程:以操作为中心 面向对象:以数据为中心,3,3,3,数据与操作:面向过程观点,程序就是对数据进行一系列的操作 先表示数据:常量,变量 再来处理数据 x = 1 y = 2 z = x + y print z 特点:数据与操作分离 数据是被动的,操作是主动的,4,4,以过程为中心,准备好数据之后,主要是进行数据处理过程的设计,即算法设计. 这种设计方法中,数据通常对整个处理过程都是公开的,不能隐藏数据. x = 1 y = 2 z = x + y w = x y # 和上一行处理同

2、样的数据x,y z = z * w print z,5,5,复杂处理过程的设计,模块化 def op1(a,b): return a * a - b * b def op2(a,b): return a * b + b * a x = 1 y = 2 z = 3 result1 = op1(x,y) result2 = op2(x,z) print result1 + result2,函数可以看作是更高 抽象级的操作,与普通 操作似乎并无本质差别. x = 1 y = 2 res = x + y print res 但函数有数据隐藏功能,6,6,函数与数据隐藏,函数是功能黑箱 使用者需要的只是

3、函数的功能,并不需要知晓它内部是如何实现功能的 函数内部处理的数据不对函数外部开放 一个函数不能直接访问另一个函数内部的数据,7,7,小结:面向过程设计,数据与操作分离,以操作过程为中心 先表示数据 主要精力放在设计数据操作及其流程控制 对复杂程序采用自顶向下设计和模块化设计 将使用低级别操作的复杂过程设计成使用高级别操作的简单过程 不适合的应用:如GUI程序 没有明确的执行流程,由不可预知的事件驱动处理过程,8,8,数据与操作:面向对象观点,数据与操作不可分离 数据类型概念已经提示我们:特定数据值和特定操作是不可分割的两件事情 Q:请问x+y是什么意思? A:+对不同类型的x,y值有不同意义

4、. 既然如此,何不将特定数据值 与特定操作捆绑在一起,形成一 种新型数据? 由此产生了对象概念,9,9,对象,对象(Object):集数据与操作于一身. 对象拥有特定数据 对象能对其数据进行特定操作 计算:向对象发操作请求消息. 对象是主动的,自己掌控对其数据的操作 对象将自己能执行的操作对外公开 面向对象(Object-Oriented):软件系统由各种对象组成,对象之间通过消息进行交互.,10,10,对象例子,人 数据:姓名,出生日期,身高,体重,. 操作:计算年龄,判断体重是否标准,. 电视机 数据:型号,厂商,尺寸,频道数,. 操作:开机,关机,调频道,调音量,. 室内环境 数据:温度

5、,湿度,容积,. 操作:调节温度,调节湿度,换算容积单位,11,11,面向过程vs面向对象编程,假设一个问题涉及数据X和Y,对X要进行的操作为f(),g(),对Y的操作为h() 面向过程设计和面向对象得到的程序分别形如,X = . Y = . f(X) g(X) h(Y),X f() g(),Y h(),请求X执行f() 请求X执行g() 请求Y执行h(),12,12,12,复杂数据的表示,简单数据可以用现成的数据类型表示 每个数据类型都包括 定义一个值的集合:如int 定义一些对值的运算(操作):如+,-,*,/ 复杂数据如何表示? 拆成简单数据 例如学生拆成name,age,addr等简单

6、数据 定义新类型 例如定义类型S,其值是由name,age,addr等构成的整体,13,13,13,自定义类型的值和操作,分离 融为一体,14,14,14,从类型到类,类是类型概念的发展 对象是广义的数据值 对象所属的数据类型就是类 用于描述复杂数据的静态和动态行为 类(class):描述相似对象的共性.包括 数据 操作:方法(method) 对象是类的实例,15,15,15,类与抽象,类是对现实事物的抽象 数据抽象 例如:从具体学生抽象出姓名,年龄,地址等数据 行为抽象 例如:从学生日常行为抽象出选课,加入社团等操作 于是产生了类Student的定义 抽象可以在多个层次上进行 例如:学生-人

7、-动物-生物,16,16,16,封装,封装:数据和操作结合成一个程序单元,对外部隐藏内部实现细节. 不允许用户直接操作类中被隐藏的信息 用户也无需了解隐藏的信息就能使用该类 类对外公开方法名称和调用格式,即界面. 外界向对象发消息(方法名及参数) 对象响应消息,执行相应方法 外界只能按对象允许的方式来处理对象数据,17,17,17,封装的好处,安全:对象自己的方法处理自己的数据 易用:使用者无需了解内部实现细节 易维护:实现者修改内部实现不会影响使用者 标准化:同类甚至不同类的对象对使用者都呈现同样的操作界面,类的定义,类定义 class : 方法定义同函数定义 def (): . 方法是依附

8、于类的函数,普通函数则是独立的. 方法的第一个参数是专用的,习惯用名字self. 具体含义见后. 只能通过向对象发消息来调用方法.,18,实例变量(1),回忆:对象是数据和操作的结合. 类定义中,方法对应于操作.数据呢? 对象的数据以实例变量形式定义. 实例变量 self. 在方法中定义 self. = 主要出现在_init_()方法中,19,实例变量(2),每个类实例(对象)具有自己的实例变量副本,用来存储该对象自己的数据. 对实例变量的访问: . 实例变量与函数局部变量不同! 同一个类的各个方法都可以访问实例变量. 类的方法中也可以定义局部变量,不能被其他方法访问.,20,21,21,21

9、,例:类定义,类Person(可单独保存为模块person.py) class Person: def _init_(self,n,y): = n self.year = y def whatName(self): print My name is, def howOld(self,y): age = y self.year if age 0: print My age in,y,is,age else: print I was born in,self.year,实例创建(1),类与实例:抽象与具体. 人是类,张三是人的实例 一个类可以创建任意多个实例

10、各实例具有相同的行为:由方法决定 但具有不同的数据:由实例变量决定 实例创建 = () 这里相当于一个函数,称为构造器,用来构造实例.,22,实例创建(2),创建时对实例进行初始化 用构造器创建实例时,系统会自动调用_init_方法 通常在此方法中执行一些初始化操作 _init_所需的参数由构造器提供. 例如: from person import Person p1 = Person(Lucy,2005),23,例:实例创建(1),创建一个Person实例 from person import Person p1 = Person(Lucy,2005),24,例:实例创建(2),创建两个Pe

11、rson实例 from person import Person p1 = Person(Lucy,2005) p2 = Person(Tom,1990),25,方法调用,类似函数调用,但需指明实例(对象). .() 就是与形参self对应的实参. 例如 p1.whatName() My name is Lucy p2.whatName() My name is Tom p2.howOld(2013) My age in 2013 is 23,26,方法调用图示, p1.whatName() p2.howOld(2013),27,编程案例:模拟炮弹飞行(1),程序规格 输入:炮弹的发射角度,初

12、速和高度 输出:炮弹的射程 解决方法:模拟炮弹飞行过程,即计算每一时刻炮弹的位置. 连续运动的离散化 时间:t, t+t, t+2t, t+3t, . 轨迹: (xt,yt), (xt+t,yt+t), .,28,编程案例:模拟炮弹飞行(2),算法: 输入角度angle(度),初速v(米/秒),高度h0(米),间隔t(秒) 将angle换算成弧度单位的theta xv = v * cos(theta) yv = v * sin(theta) 初始位置(xpos,ypos) = (0,h0) 当炮弹还未落地(即ypos = 0.0): 更新炮弹位置(xpos,ypos) 更新yv 输出xpos,

13、29,编程案例:模拟炮弹飞行(3),核心代码:位置更新 水平方向 xpos = xpos + xv * t 垂直方向 yv1 = yv - 9.8 * t ypos = ypos + (yv + yv1) / 2.0 * t,30,from math import pi, sin, cos def main(): angle = input(Enter the launch angle: ) vel = input(Enter the initial velocity: ) h0 = input(Enter the initial height : ) time = input(Enter t

14、he time interval : ) radians = (angle * pi)/180.0 xpos = 0 ypos = h0 xvel = vel * cos(radians) yvel = vel * sin(radians) while ypos = 0: xpos = xpos + time * xvel yvel1 = yvel - 9.8 * time ypos = ypos + time * (yvel + yvel1)/2.0 yvel = yvel1 print nDistance traveled: %0.1f meters. % (xpos) main() 这个

15、版本是流水帐式的,没有章法结构. 程序不长,倒有10个变量,为理解程序需要跟踪这10个数据的变化.,编程案例:模拟炮弹飞行(4),模块化版本cball2.py 主程序(主函数)非常简洁,易理解. def main(): angle, v, h0, t = getInputs() xv, yv = getXY(v,angle) xpos = 0 ypos = h0 while ypos = 0: xpos,ypos,yv = update(t,xpos,ypos,xv,yv) print 射程: %0.1f 米. % (xpos) 将辅助性变量(theta和yv1)隐藏在辅助函数中. 仍然不够好

16、:update函数界面太复杂.,32,编程案例:模拟炮弹飞行(5),面向对象版本cball3.py 炮弹是现实实体,用xpos,ypos,xv和yv四个分离的数据来描述它是“只见树木不见森林”. OOP:将炮弹的信息和行为都封装在类中,并创建一个炮弹对象,程序会更加简洁易理解. def main(): angle, vel, h0, time = getInputs() cball = Projectile(angle, vel, h0) while cball.getY() = 0: cball.update(time) print 射程: %0.1f 米. % (cball.getX()

17、所有复杂性都隐藏在类中了,33,from math import pi, sin, cos class Projectile: def _init_(self, angle, velocity, height): self.xpos = 0.0 self.ypos = height theta = pi * angle / 180.0 self.xvel = velocity * cos(theta) self.yvel = velocity * sin(theta) def update(self, time): self.xpos = self.xpos + time * self.xve

18、l yvel1 = self.yvel - 9.8 * time self.ypos = self.ypos + time * (self.yvel + yvel1) / 2.0 self.yvel = yvel1 def getY(self): return self.ypos def getX(self): return self.xpos,编程案例:模拟炮弹飞行(6),三个版本体现的思想变迁,35,类与模块化,复杂程序的模块化设计 功能分解:利用子程序(如函数)概念,以过程为中心设计功能模块 数据分解:利用类的概念,以数据为中心设计数据模块 功能模块不太适合复杂数据的处理 类模块独立性更

19、高,可重用性更好 类定义可以提供给任何程序使用 很多OO语言都提供类库,36,例:学生信息处理系统,功能分解 课程注册模块,修改学生信息模块,成绩登录模块等 每个模块(函数)都需要了解”学生”数据的细节 数据分解 创建”学生”类S,隐藏数据和操作实现细节,使用者无需了解内部细节就能执行操作 其他数据模块包括”课程”类,”教师”类等,37,如何表示任意复杂的数据,数据的复杂性表现在 数量大:用集合体数据类型来表示 有内部深层结构:用类来表示 两种复杂性混合:用对象的集合来刻画 people = p1, p2 for p in people: p.whatName() p.howOld(2013)

20、,38,39,39,39,超类与子类*,子类 继承 覆写 多态,40,40,40,面向对象设计*,OOD:对给定问题找出并定义一组有用的类的过程. 基于词性分析的一种方法 问题描述 描述中的名词(事物) 问题描述中的动词(对象行为),41,End,Functions that Modify Parameters,Return values are the main way to send information from a function back to the caller. Sometimes, we can communicate back to the caller by maki

21、ng changes to the function parameters. Understanding when and how this is possible requires the mastery of some subtle details about how assignment works and the relationship between actual and formal parameters.,Functions that Modify Parameters,Suppose you are writing a program that manages bank ac

22、counts. One function we would need to do is to accumulate interest on the account. Lets look at a first-cut at the function. def addInterest(balance, rate): newBalance = balance * (1 + rate) balance = newBalance,Functions that Modify Parameters,The intent is to set the balance of the account to a ne

23、w value that includes the interest amount. Lets write a main program to test this:def test(): amount = 1000 rate = 0.05 addInterest(amount, rate) print amount,Functions that Modify Parameters,We hope that that the 5% will be added to the amount, returning 1050. test()1000 What went wrong? Nothing!,F

24、unctions that Modify Parameters,The first two lines of the test function create two local variables called amount and rate which are given the initial values of 1000 and 0.05, respectively.,def addInterest(balance, rate): newBalance = balance * (1 + rate) balance = newBalance def test(): amount = 10

25、00 rate = 0.05 addInterest(amount, rate) print amount,Functions that Modify Parameters,Control then transfers to the addInterest function. The formal parameters balance and rate are assigned the values of the actual parameters amount and rate. Even though rate appears in both, they are separate vari

26、ables (because of scope rules).,def addInterest(balance, rate): newBalance = balance * (1 + rate) balance = newBalance def test(): amount = 1000 rate = 0.05 addInterest(amount, rate) print amount,Functions that Modify Parameters,The assignment of the parameters causes the variables balance and rate

27、in addInterest to refer to the values of the actual parameters!,def addInterest(balance, rate): newBalance = balance * (1 + rate) balance = newBalance def test(): amount = 1000 rate = 0.05 addInterest(amount, rate) print amount,Functions that Modify Parameters,Functions that Modify Parameters,Execut

28、ing the first line of addInterest creates a new variable, newBalance. balance is then assigned the value of newBalance.,def addInterest(balance, rate): newBalance = balance * (1 + rate) balance = newBalance def test(): amount = 1000 rate = 0.05 addInterest(amount, rate) print amount,Functions that M

29、odify Parameters,balance now refers to the same value as newBalance, but this had no effect on amount in the test function.,def addInterest(balance, rate): newBalance = balance * (1 + rate) balance = newBalance def test(): amount = 1000 rate = 0.05 addInterest(amount, rate) print amount,Functions th

30、at Modify Parameters,Functions that Modify Parameters,Execution of addInterest has completed and control returns to test. The local variables, including the parameters, in addInterest go away, but amount and rate in the test function still refer to their initial values!,def addInterest(balance, rate

31、): newBalance = balance * (1 + rate) balance = newBalance def test(): amount = 1000 rate = 0.05 addInterest(amount, rate) print amount,Functions that Modify Parameters,To summarize: the formal parameters of a function only receive the values of the actual parameters. The function does not have acces

32、s to the variable that holds the actual parameter. Python is said to pass all parameters by value.,Functions that Modify Parameters,Some programming languages (C+, Ada, and many more) do allow variables themselves to be sent as parameters to a function. This mechanism is said to pass parameters by r

33、eference. When a new value is assigned to the formal parameter, the value of the variable in the calling program actually changes.,Functions that Modify Parameters,Since Python doesnt have this capability, one alternative would be to change the addInterest function so that it returns the newBalance.

34、,Functions that Modify Parameters,def addInterest(balance, rate): newBalance = balance * (1 + rate) return newBalance def test(): amount = 1000 rate = 0.05 amount = addInterest(amount, rate) print amount test(),Functions that Modify Parameters,Instead of looking at a single account, say we are writi

35、ng a program for a bank that deals with many accounts. We could store the account balances in a list, then add the accrued interest to each of the balances in the list. We could update the first balance in the list with code like:balances0 = balances0 * (1 + rate),Functions that Modify Parameters,Th

36、is code says, “multiply the value in the 0th position of the list by (1 + rate) and store the result back into the 0th position of the list.” A more general way to do this would be with a loop that goes through positions 0, 1, , length 1.,Functions that Modify Parameters,def addInterest(balances, ra

37、te): for i in range(len(balances): balancesi = balancesi * (1+rate) def test(): amounts = 1000, 2200, 800, 360 rate = 0.05 addInterest(amounts, rate) print amounts test(),Functions that Modify Parameters,Remember, our original code had these values:1000, 2200, 800, 360 The program returns:1050.0, 23

38、10.0, 840.0, 378.0 What happened? Python passes parameters by value, but it looks like amounts has been changed!,Functions that Modify Parameters,The first two lines of test create the variables amounts and rate. The value of the variable amounts is a list object that contains four int values.,def addInterest(balances, rate): for i in range(len(balances): balancesi = balancesi * (1+rate) def test(): amounts = 1000, 2200, 800, 360 rate = 0.05 addInterest(amounts, rate) print amounts,Functions that Modify Parameters,Functions that Modify Parameters,Next, addInterest execu

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论