你说的是object oriented programming。我建议你在大学或online上这门课。不过,我还是花了点时间举了一个简单的例子来说明我认为你希望它做什么:class A(object):
def __init__(self):
print("hello world")
def new_print(self, some_word):
print(some_word.swapcase())
@staticmethod
def newer_print(some_word):
print(some_word.lower())
class B(object):
def __init__(self):
print("world")
#create the object of Class A and then call the method
temp = A()
temp.new_print("This is a test")
#call the static method of Class A
A.newer_print("Just one more test")
if __name__ == "__main__":
#create the object for Class B
test = B()
注意Class A有两个方法(除了__init__)。第一个(new_print)要求在调用方法之前实例化该类的对象。第二个(newer_print)可以静态运行。
调用另一个类方法只需执行以下操作之一:创建类的对象并调用其方法,或者
调用类的静态方法
如果你看看B的实例化方法,你会发现这两个例子已经演示过了。