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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ws 无法热替换的问题

    ws 无法热替换的问题

    2022年2月23日
    39
  • 智能车浅谈——硬件篇

    智能车浅谈——硬件篇浅谈智能车制作

    2022年7月22日
    14
  • Pyhton Cookbook 学习笔记 ch9_02 元编程[通俗易懂]

    Pyhton Cookbook 学习笔记 ch9_02 元编程[通俗易懂]【传送门】9.8将装饰器定义为类的一部分问题:想在类中定义装饰器,并作用在其他的函数上方案:在类中定义装饰器首先要确定它的使用方法,是作为一个实例方法还是作为一个类方法fromfunctoolsimportwrapsclassA:#作为一个实例方法defdecorator1(self,func):@wraps(func)…

    2022年6月3日
    35
  • 2018以太坊编程语言solidity最佳IDE

    2018以太坊编程语言solidity最佳IDE

    2021年6月11日
    147
  • GoogLeNet v1结构「建议收藏」

    GoogLeNet v1结构「建议收藏」GooLeNetInception块GooLeNetGooLeNet是google2014年推出的深度神经网络模型,原论文名称为:Goingdeeperwithconvolutions,在沐神的书上也被叫做含并⾏连结的⽹络(主要由于Inception块中的结构)。该模型在2014年ILSVRC2014比赛中竞赛中夺得冠军,14年之后也在不断发展,这里就介绍一下初始的版本。Inception块GooLeNet思想有点类似于VGG,先构建块,网络主要通过块的堆叠实现,不过这里的块与VGG块不同,称

    2022年8月14日
    11
  • releasecapture 函数_整理怎么解释

    releasecapture 函数_整理怎么解释setCapture一.什么是setCapture函数?MDN解释:在处理一个mousedown事件过程中调用这个方法来把全部的鼠标事件重新定向到这个元素,直到鼠标按钮被释放或者document.releaseCapture()被调用。函数作用:程序中主要是要捕获onmousemove和onmouseup事件语法:element.setCapture(retargetToElement);如果被设置为true,所有事件被直接定向到这个元素;如果是false,事件也可以在这

    2022年5月3日
    47

发表回复

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

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