上海交通大学python程序设计课程.pptx
文本预览下载声明
面向对象思想和编程;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;类的定义;实例变量(1);实例变量(2);21;实例创建(1);实例创建(2);例:实例创建(1);例:实例创建(2);方法调用;方法调用图示;编程案例:模拟炮弹飞行(1);编程案例:模拟炮弹飞行(2);编程案例:模拟炮弹飞行(3);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);编程案例:模拟炮弹飞行(5);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);类与模块化;例:学生信息处理系统;如何表示任意复杂的数据;39;40;41;Functions that Modify Parameters;Functions that Modify Parameters;Functions that Modify Parameters;Functions that Modify Parameters;Functions that Modify Parameters;Functions that Modify Parameters;Functions that Modify Parameters;Functions that Modify Parameters;Functions that Modify Parameters;Functions that Modify Parameters;Functions that Modify Parameters;Functions that Modify Parameters;Functions that Modify Parameters;Functions that Modify Parameters;Functions that Modify Parameters;Functions that Modify Parameters;Functions that Modify Parameters;Functions that Modify Parameters;Functions that Modif
显示全部