python编程设计大学ppt_上海交通大学-python程序设计课程PPT-Ch7-(1)...ppt

上海交通大学-python程序设计课程PPT-Ch7-(1)...ppt

面向对象思想和编程,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 # 和上一行处理同样的数据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,函数与数据隐藏,函数是功能黑箱 使用者需要的只是函数的功能,并不需要知晓它内部是如何实现功能的 函数内部处理的数据不对函数外部开放 一个函数不能直接访问另一个函数内部的数据,7,7,小结:面向过程设计,数据与操作分离,以操作过程为中心 先表示数据 主要精力放在设计数据操作及其流程控制 对复杂程序采用自顶向下设计和模块化设计 将使用低级别操作的复杂过程设计成使用高级别操作的简单过程 不适合的应用:如GUI程序 没有明确的执行流程,由不可预知的事件驱动处理过程,8,8,数据与操作:面向对象观点,数据与操作不可分离 数据类型概念已经提示我们:特定数据值和特定操作是不可分割的两件事情 Q:请问x+y是什么意思? A:+对不同类型的x,y值有不同意义. 既然如此,何不将特定数据值 与特定操作捆绑在一起,形成一 种新型数据? 由此产生了对象概念,9,9,对象,对象(Object):集数据与操作于一身. 对象拥有特定数据 对象能对其数据进行特定操作 计算:向对象发操作请求消息. 对象是主动的,自己掌控对其数据的操作 对象将自己能执行的操作对外公开 面向对象(Object-Oriented):软件系统由各种对象组成,对象之间通过消息进行交互.,10,10,对象例子,人 数据:姓名,出生日期,身高,体重,. 操作:计算年龄,判断体重是否标准,. 电视机 数据:型号,厂商,尺寸,频道数,. 操作:开机,关机,调频道,调音量,. 室内环境 数据:温度,湿度,容积,. 操作:调节温度,调节湿度,换算容积单位,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等简单数据 定义新类型 例如定义类型S,其值是由name,age,addr等构成的整体,13,13,13,自定义类型的值和操作,分离 融为一体,14,14,14,从类型到类,类是类型概念的发展 对象是广义的数据值 对象所属的数据类型就是类 用于描述复杂数据的静态和动态行为 类(class):描述相似对象的共性.包括 数据 操作:方法(method) 对象是类的实例,15,15,15,类与抽象,类是对现实事物的抽象 数据抽象 例如:从具体学生抽象出姓名,年龄,地址等数据 行为抽象 例如:从学生日常行为抽象出选课,加入社团等操作 于是产生了类Student的定义 抽象可以在多个层次上进行 例如:学生-人-动物-生物,16,16,16,封装,封装:数据和操作结合成一个程序单元,对外部隐藏内部实现细节. 不允许用户直接操作类中被隐藏的信息 用户也无需了解隐藏的信息就能使用该类 类对外公开方法名称和调用格式,即界面. 外界向对象发消息(方法名及参数) 对象响应消息,执行相应方法 外界只能按对象允许的方式来处理对象数据,17,17,17,封装的好处,安全:对象自己的方法处理自己的数据 易用:使用者无需了解内部实现细节 易维护:实现者修改内部实现不会影响使用者 标准化:同类甚至不同类的对象对使用者都呈现同样的操作界面,类的定义,类定义 class : 方法定义同函数定义 def (): . 方法是依附于类的函数,普通函数则是独立的. 方法的第一个参数是专用的,习惯用名字self. 具体含义见后. 只能通过向对象发消息来调用方法.,18,实例变量(1),回忆:对象是数据和操作的结合. 类定义中,方法对应于操作.数据呢? 对象的数据以实例变量形式定义. 实例变量 self. 在方法中定义 self. = 主要出现在__init__()方法中,19,实例变量(2),每个类实例(对象)具有自己的实例变量副本,用来存储该对象自己的数据. 对实例变量的访问: . 实例变量与函数局部变量不同! 同一个类的各个方法都可以访问实例变量. 类的方法中也可以定义局部变量,不能被其他方法访问.,20,21,21,21,例:类定义,类Person(可单独保存为模块person.py) class Person: def __init__(self,n,y): self.name = n self.year = y def whatName(self): print My name is,self.name 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),类与实例:抽象与具体. 人是类,张三是人的实例 一个类可以创建任意多个实例 各实例具有相同的行为:由方法决定 但具有不同的数据:由实例变量决定 实例创建 = () 这里相当于一个函数,称为构造器,用来构造实例.,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),创建两个Person实例 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),程序规格 输入:炮弹的发射角度,初速和高度 输出:炮弹的射程 解决方法:模拟炮弹飞行过程,即计算每一时刻炮弹的位置. 连续运动的离散化 时间: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,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 the 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() 这个版本是流水帐式的,没有章法结构. 程序不长,倒有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)隐藏在辅助函数中. 仍然不够好: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() 所有复杂性都隐藏在类中了,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.xvel 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,类与模块化,复杂程序的模块化设计 功能分解:利用子程序(如函数)概念,以过程为中心设计功能模块 数据分解:利用类的概念,以数据为中心设计数据模块 功能模块不太适合复杂数据的处理 类模块独立性更高,可重用性更好 类定义可以提供给任何程序使用 很多OO语言都提供类库,36,例:学生信息处理系统,功能分解 课程注册模块,修改学生信息模块,成绩登录模块等 每个模块(函数)都需要了解”学生”数据的细节 数据分解 创建”学生”类S,隐藏数据和操作实现细节,使用者无需了解内部细节就能执行操作 其他数据模块包括”课程”类,”教师”类等,37,如何表示任意复杂的数据,数据的复杂性表现在 数量大:用集合体数据类型来表示 有内部深层结构:用类来表示 两种复杂性混合:用对象的集合来刻画 people = p1, p2 for p in people: p.whatName() p.howOld(2013),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 making 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 accounts. 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 new 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!,Functions 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 = 1000 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 variables (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 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,Executing 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 Modify 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 that 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): 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 access 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 reference. 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.,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 writing 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,This 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, 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 test(),Functions that Modify Parameters,Remember, our original code had these values:1000, 2200, 800, 360 The program returns:1050.0, 2310.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 executes. The loop goes through each index in the range 0, 1, , length 1 and updates that value in balances.,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,In the diagram the old values are left hanging around to emphasize that the numbers in the boxes have not changed, but the new values were created and assigned into the list. The old values will be destroyed during garbage collection.,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,When addInterest terminates, the list stored in amounts now contains the new values. The variable amounts wasnt changed (its still a list), but the state of that list has changed, and this change is visible to the calling program.,Functions that Modify Parameters,Parameters are always passed by value. However, if the value of the variable is a mutable object (like a list of graphics object), then changes to the state of the object will be visible to the calling program

你可能感兴趣的:(python编程设计大学ppt)