微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我的方法返回值链接处的地址

如何解决我的方法返回值链接处的地址

这是一个不属于现场培训的练习,我不明白为什么当我想显示我的卫星类的能量方法的返回值时,我在控制台上收到一个地址。

>
Zoe satellite speed = 40.0m / s.
<bound method Satellite.energie of <__ main __. Satellite object at 0x01A5E610 >>
Zoe satellite speed = 70.0m / s.
<bound method Satellite.energie of <__ main __. Satellite object at 0x01A5E610 >>

这是我的代码

class Satellite (object):
    "" "Satellite for instantiating objects simulating satellites
artificial launched into space,around the earth. "" "

    def __init __ (self,name,mass = 100,speed = 0):
        self.name = name
        self.mass = mass
        speed choke = speed

    def impulse (self,force,duration):
        "" "will vary the speed of the satellite." ""

        speed self = speed self + (force * duration) / mass self

    def energy (self):
        "" "will refer to the program calling the kinetic energy value of the satellite" ""
        return_val = self.mass * self.speed ** 2/2
        return return_val

    def display_speed (self):
        "" "will display the name of the satellite and its current speed." ""
        print ("satellite speed {0} = {1} m / s.". format (self.name,self.speed))


s1 = Satellite ('Zoe',mass = 250,speed = 10)
s1.pulse (500,15)
s1.display_speed ()
print (s1.energy)
s1.pulse (500,15)
s1.display_speed ()
print (s1.energy)

解决方法

你的 energy 方法是错误的,它应该给出了一个语法错误:

def energy (self):
    "" "will refer to the program calling the kinetic energy value of the satellite" ""
    return_val = self.mass * self.speed ** 2/2
    return return_val

更不用说,您还需要调用 energy 方法:

s1.pulse (500,15)
s1.display_speed ()
print(s1.energy())
s1.pulse (500,15)
s1.display_speed ()
print(s1.energy())

您试图覆盖 return 语句并将其用作变量,这是错误的。您的代码中还有其他错误输入/问题,但这个似乎绝对是主要问题。

,

您不是调用函数 Satellite.energy(),而是尝试访问属性 Satellite.energy - 如果将其更改为 print(s1.energy()),它应该会正确显示!

请注意,您的某些代码在此处格式错误,因此无法运行 - 这对我有用:

class Satellite (object):
    "" "Satellite for instantiating objects simulating satellites artificial launched into space,around the earth. "" "

    def __init__ (self,name,mass = 100,speed = 0):
        self.name = name
        self.mass = mass
        self.speed = speed

    def impulse (self,force,duration):
        "" "will vary the speed of the satellite." ""

        return self.speed + (force * duration) / self.mass

    def energy (self):
        "" "will refer to the program calling the kinetic energy value of the satellite" ""
        return self.mass * self.speed ** 2/2

    def display_speed (self):
        "" "will display the name of the satellite and its current speed." ""
        print ("satellite speed {0} = {1} m / s.". format (self.name,self.speed))


s1 = Satellite ('Zoe',mass = 250,speed = 10)
s1.impulse (500,15)
s1.display_speed ()
print (s1.energy())
s1.impulse (500,15)
s1.display_speed ()
print (s1.energy())

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?