Python:类的定义与使用
Python,定义,使用
2025-04-07 09:01:27 时间
大家好,又见面了,我是你们的朋友全栈君。
类的定义与使用
cball = Projectile(angle, vel, h0)
中, cball
传入给self
一个炮弹从某个倾角射出计算水平位移和大致飞行时间的程序
# projectile.py
from math import radians, sin , cos
##############类的定义#######
class Projectile():
def __init__(self, angle, velocity, height):
self.xpos = 0.0
self.ypos = height
theta = radians(angle)
self.xvel = velocity * cos(theta)
self.yvel = velocity * sin(theta)
self.totaltime = 0.0
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
self.totaltime = self.totaltime + time
def getX(self):
return self.xpos
def getY(self):
return self.ypos
#############################
###############函数定义########
def getIputs():
a = float(input("Enter the lanuch angle (in degrees):"))
v = float(input("Enter the lanuch velocity (in meters/sec):"))
h = float(input("Enter the lanuch height (in meters):"))
t = float(input("Enter the internal between position calculations:"))
return a, v, h, t
def main():
angle, vel, h0, time = getIputs()
cball = Projectile(angle, vel, h0)
while cball.getY() >= 0:
cball.update(time)
print("\nDistance traveled: {0:0.1f} meters.".format(cball.getX()))
print("\nTotal time spent is roughly: {0:0.1f} sceonds.".format(cball.totaltime))
#############################
###########函数调用###########
if __name__ == '__main__':
main()
运行示例:
'''
Enter the lanuch angle (in degrees):50
Enter the lanuch velocity (in meters/sec):1
Enter the lanuch height (in meters):50
Enter the internal between position calculations:1
Distance traveled: 2.6 meters.
Total time spent is roughly: 4.0 sceonds.
'''
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/156119.html原文链接:https://javaforall.cn
相关文章
- Python中的取整、取余运算「建议收藏」
- Python <算法思想集结>之抽丝剥茧聊动态规划
- python中sqrt函数用法_Python : sqrt() 函数
- python计算最大公约数和最小公倍数_python怎么求最大公约数和最小公倍数
- 用Python的好处
- 使用Python检测符号及乱码字符
- python中的pop函数和append函数
- 你一定是在逗我,Python都不会就想做算法?
- Python 树表查找_千树万树梨花开,忽如一夜春风来(二叉排序树、平衡二叉树)
- 《快学Python》送书名单!
- python实现微信发消息
- webstorm占用内存过高_python程序内存不断增加
- Python版本的选择
- 如何利用Python画图
- python 基尼系数_Python计算
- 2022年最新Python大数据之Python基础【九】面向对象与继承
- 用Python爬了微信好友,原来他们是这样的人...
- python执行cmd命令并解析结果_python如何打包成可执行程序
- python 字符串转成数字_python数字转十六进制字符串
- python 爬取图集谷妹子图片,按自己喜好抓取一页图片,有兴趣二次开发 抓全站