簡體   English   中英

從同一類中的不同方法創建的對象上調用類中的方法?

[英]Call a method from a class on something created by a different method in the same class?

我想從類中的方法調用類中的另一個方法創建的東西。 我該怎么做?

例如,我剛剛創建了一個名為call me的類,並想在add方法中使用subtract方法的結果。

class call_me(object):

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

   def subtract(self):
        difference = self.y - self.x
        return difference

   def add(self.subtract,second_number):
        # code to add the difference returned by subtract to the second number.

如何將第二個數字加到減法返回的差值中? 有什么辦法可以add difference嗎?

def add(self, second_number):
    difference = self.substract()
    return difference + second_number

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM