我正在尝试创建一个程序,该程序将对象打印出两个不同组中的人员列表

时间:2017-01-02 03:31:16

标签: python

class CRunners(CharityRunner):

#methods

    def charityrunner():
        print ("Club: Charity Runners")
        print ("Runner: HRU001 John Richards")
        print ("Runner: HRU004 Alicia")
        print ("Runner: HRU005 Mo Farah")


class PRunners(ProfessionalRunner):

    def professionalrunner():
        print ("Club: Professional Runners")
        print ("Runner: HRU055 Sarch Richards")
        print ("Runner: HRU042 David")
        print ("Runner: HRU033 Moe Size")

class calling():

    def calling2(self):
        professionalrunner()
        charityrunner()


calling2()

这里我试图让它这样我可以打印这两个作为一个功能,但我不完全确定如何做到这一点

2 个答案:

答案 0 :(得分:1)

你似乎已经学会了一种语言,它要求所有东西都在课堂上(比如Java)。在Python中,您不需要这些类,您的程序将更容易理解如下:

def charityrunner():
    print ("Club: Charity Runners")
    print ("Runner: HRU001 John Richards")
    print ("Runner: HRU004 Alicia")
    print ("Runner: HRU005 Mo Farah")

def professionalrunner():
    print ("Club: Professional Runners")
    print ("Runner: HRU055 Sarch Richards")
    print ("Runner: HRU042 David")
    print ("Runner: HRU033 Moe Size")

def calling2():
    professionalrunner()
    charityrunner()

calling2()

答案 1 :(得分:0)

您可以使用@staticmethod定义您的方法,因此在调用它们时只需使用class_name.method_name

class CRunners(CharityRunner):
    @staticmethod 
    def charityrunner():
        ...
class PRunners(ProfessionalRunner):
    @staticmethod 
    def professionalrunner():
        ...
class calling():
    @staticmethod
    def calling2():
        CRunners.professionalrunner()
        PRunners.charityrunner()

calling.calling2()