Python:类的定义与使用

Python:类的定义与使用类的定义与使用cball=Projectile(angle,vel,h0)中,cball传入给self一个炮弹从某个倾角射出计算水平位移和大致飞行时间的程序#projectile.p

大家好,又见面了,我是你们的朋友全栈君。

类的定义与使用

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.net/156119.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 2021年6月最新面试记录

    2021年6月最新面试记录

    2022年2月19日
    50
  • python+opencv图像模板匹配—单模板匹配

    python+opencv图像模板匹配—单模板匹配

    2021年10月6日
    38
  • Git使用教程:最详细、最傻瓜、最浅显、真正手把手教!「建议收藏」

    转载自Git使用教程预警:因为详细,所以行文有些长,新手边看边操作效果出乎你的预料)一:Git是什么?Git是目前世界上最先进的分布式版本控制系统。工作原理/流程:Workspace:工作区Index/Stage:暂存区Repository:仓库区(或本地仓库)Remote:远程仓库二:SVN与Git的最主要的区别?SVN是集中式版本控制系统,版本库是集中放在中央…

    2022年4月6日
    42
  • 静态测试和动态测试的区别在哪里?_软件测试中的静态测试

    静态测试和动态测试的区别在哪里?_软件测试中的静态测试1.静态测试静态测试(statictesting)就是不实际运行被测软件,而只是静态地检查程序代码、界面或文档中可能存在的错误的过程。包括对代码测试、界面测试和文档测试三个方面:    对于代码测试,主要测试代码是否符合相应的标准和规范。    对于界面测试,主要测试软件的实际界面与需求中的说明是否相符。    对于文档测试,主要测试用户手册和需求说明是否符合用户的实际需求。…

    2025年7月29日
    6
  • js获取当前系统时间

    js获取当前系统时间js获取当前年,月,日,时分秒,季度,星期几

    2022年10月18日
    2
  • Keras入门(八)K折交叉验证

    Keras入门(八)K折交叉验证在文章 Keras 入门 一 搭建深度神经网络 DNN 解决多分类问题中 笔者介绍了如何搭建 DNN 模型来解决 IRIS 数据集的多分类问题 本文将在此基础上介绍如何在 Keras 中实现 K 折交叉验证 什么是 K 折交叉验证 K 折交叉验证是机器学习中的一个专业术语 它指的是将原始数据随机分成 K 份 每次选择 K 1 份作为训练集 剩余的 1 份作为测试集 交叉验证重复 K 次 取 K 次准确率的平均值作为最终模型的评价指标 一般取 K 10 即 10 折交叉验证 如下图所示 用交叉验证的目的是为了得到可靠稳定的模型 K 折交

    2025年6月14日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号