【问题标题】:How to call on a class using 3 functions?如何使用 3 个函数调用一个类?
【发布时间】:2021-10-01 05:31:34
【问题描述】:

我想创建一个包含 3 个函数的 Python 类。

  • 功能 1 将要求用户输入一个数字。
  • 函数 2 将要求用户输入另一个数字。
  • 函数 3 将函数 1 * 函数 2 相乘并返回乘积。

这是我目前的代码:

class Product:

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def get_x(self):
        x = int(input('What is the first number?: ')

    def get_y(self):
        y = int(input('What is the second number?: ')

    def mult_XY(self):
        return x * y  

当我尝试调用课程时,我得到'y' is not defined

我尝试使用以下代码:

num = Product(x, y)
print(num.mult_XY)

【问题讨论】:

  • 使用这种方法,每个变量都需要self.
  • 连括号,如果不关闭int()语句,会抛出错误:y = int(input('What is the second number?: ')

标签: python class methods


【解决方案1】:

这是一个可行的解决方案。将其与您当前的解决方案进行比较并发现差异。在下面的代码 sn-p 之后,我将重点介绍您需要研究的概念,以便更好地理解这个程序。

这是您的代码的正确版本(注意:可能有不止一种解决方案):

class Product:

    def __init__(self):
        return

    def get_x(self):
        self.x = int(input('What is the first number?: '))
        return self.x
    
    def get_y(self):
        self.y = int(input('What is the second number?: '))
        return self.y

    def mult_XY(self):
        return self.x * self.y

p = Product()
x = p.get_x()
y = p.get_y()
result = p.mult_XY()
print('RESULT of {} * {} = {}'.format(x, y, result))

这是最好的答案吗?不会。根据程序的具体情况,代码的结构可能会有所不同。

您对以下概念存在知识空白:

  • Python 中的对象和类
  • Python 中的函数
  • Python 中的变量和范围

为了更好地理解,您需要更多地了解 Python 的基础知识。这是一个很好的资源,可以帮助您入门:https://python-textbok.readthedocs.io/en/1.0/Introduction.html

读完之后,你不仅可以回答这个问题,还可以为你的编程知识打下基础。不要放弃,祝你一切顺利。

【讨论】:

  • 您的代码有问题。初始化Product类时,需要xy
  • 我喜欢你的编码方式......但是,我收到 p = Product() 上的错误
  • @TonyMontana 已更新,请参阅编辑
  • 看起来您只需要在最后一行代码中再添加一个括号。这很好用@geralt0!非常感谢您的时间和解释。
  • @AbleArcher 哎呀,好消息!我用最后的括号更新了答案。不客气!享受 Python 编程之旅,享受学习乐趣!
【解决方案2】:

您似乎忘记在函数定义中使用关键字self。您的代码中有很多错误。 我认为这是您的代码的正确版本:

class Product:

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def get_x(self):
        self.x = int(input('What is the first number?: '))

    def get_y(self):
        self.y = int(input('What is the second number?: '))

    def mult_XY(self):
        return self.x * self.y

这就是你应该如何检查它的工作:

(更新版本)

x = 10
y = 5
num = Product(x, y)
num.get_x()
num.get_y()
print(num.mult_XY())

【讨论】:

    【解决方案3】:

    要引用存储在当前对象中的任何内容,您需要使用 self. 就像在 init 函数中最初保存值一样。

    例子:

    class Product:
    
        def __init__(self, x, y):
            self.x = x
            self.y = y
    
        def get_x(self):
            self.x = int(input('What is the first number?: '))
    
        def get_y(self):
            self.y = int(input('What is the second number?: '))
    
        def mult_XY(self):
            return self.x * self.y  
    

    【讨论】:

      【解决方案4】:

      我不确定我是否理解正确,但您可能需要“x”和“y”作为类中的输入。

      如果是这样,请使用classmethods:

      class Product:
          @classmethod
          def get_x(self):
              self.x = int(input('What is the first number?: '))
              return self
          @classmethod
          def get_y(self):
              self.y = int(input('What is the second number?: '))
              return self
          @classmethod
          def mult_XY(self):
              return self.x * self.y
      

      例如:

      >>> Product.get_x().get_y().mult_XY()
      What is the first number?: 3
      What is the second number?: 4
      12
      >>> 
      

      【讨论】:

      • 今天我学到了一些新东西。使用 classmethod 链接方法。以前从未想过。
      • @TonyMontana 是的!伟大的!它有时很有用。
      • 这段代码完美运行@U12-Forward!非常感谢你的回复。我会从中学到很多东西。 =)
      • @AbleArcher 如果我的回答可能对您有所帮助,请随时通过单击复选标记 (✔️) 将其标记为已接受,也可以随时使用向上的三角形进行投票。请阅读this 了解有关接受和支持答案的更多信息。
      • 此用户已成为会员三年 - 无需告诉他们接受/支持您的答案 - 他们可能知道。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-03
      • 2018-12-09
      • 2016-01-09
      相关资源
      最近更新 更多