我如何在python中继承一个类?

时间:2014-05-01 17:27:55

标签: python python-2.7

我试图将竞争对手与他们竞争的种族联系起来,如何通过继承事件类来执行show()功能?我仍然围绕着继承问题,所以如果这个问题很明显,我会道歉。

import sqlite3

print "hello"

class competitor(object):
    def __init__(self, name, dob, number, events):
        self.name = name
        self.dob = dob
        self.number = number ##this needs to check db for existing
        self.events = events

    def listEvents(self):
        for all in self.events:
            self.show()

class event(competitor):
    def __init__(self, name, distance, date, time):
        self.name = name
        self.distance = distance #meters
        self.date = date # [dd,mm,yyyy]
        self.time = time # [d,h,m,s]

    def printDate(self):
        date = str(self.date[0]) + "/" + str(self.date[1]) + "/" + str(self.date[2])
        ##print date
        return date

    def printTime(self):
        if (self.time[0] > 0):
            time = str(self.time[0]) + "." + str(self.time[1]) + ":" + str(self.time[2]) + "." + str(self.time[3])
            return time
        else:
            time = str(self.time[1]) + ":" + str(self.time[2]) + "." + str(self.time[3])
            return time

    def getKmPace(self):
        time_s = self.time[0]*3600*24 + self.time[1]*3600 + self.time[2]*60 + self.time[3]
        time_m = time_s/60.0
        pace = time_m/(self.distance/1000.0)
        return pace

    def show(self):
        print "Event: ", self.name, " Date: ", self.printDate()
        print "Distance: ",self.distance/1000.0,"KM, Time: ", self.printTime()
        print "Pace per 1 KM: ", self.getKmPace(), " minutes."


kdl = event("20KM",20000,[26,4,2014],[0,1,27,36])
kdl_bad = event("20KM",20000,[26,4,2013],[0,2,35,37])
kdl.show()

richard = competitor("Richard", 1993, 1, [kdl,kdl_bad])
richard.listEvents()

2 个答案:

答案 0 :(得分:3)

好吧,仔细考虑一下你的班级设计。 “事件”是一个更具体的“竞争对手”吗?或者“事件”是否“竞争对手?当您描述”is-a“关系时,通常会使用继承,而不是”has-a“。

在您的情况下,competitor引用了多个event个对象。您的课程设计已经在正确的轨道上。但是,你对继承的使用不是。

一个简单的解决方法:

class competitor(object):
    def __init__(self, name, dob, number, events):
        self.name = name
        self.dob = dob
        self.number = number ##this needs to check db for existing
        self.events = events

    def listEvents(self):
        for event in self.events:
            event.show()

class event(object):
    def __init__(self, name, distance, date, time):
        self.name = name
        self.distance = distance #meters
        self.date = date # [dd,mm,yyyy]
        self.time = time # [d,h,m,s]

    def printDate(self):
        date = str(self.date[0]) + "/" + str(self.date[1]) + "/" + str(self.date[2])
        return date

    def printTime(self):
        if (self.time[0] > 0):
            time = str(self.time[0]) + "." + str(self.time[1]) + ":" + str(self.time[2]) + "." + str(self.time[3])
        else:
            time = str(self.time[1]) + ":" + str(self.time[2]) + "." + str(self.time[3])
        return time

    def getKmPace(self):
        time_s = self.time[0]*3600*24 + self.time[1]*3600 + self.time[2]*60 + self.time[3]
        time_m = time_s/60.0
        pace = time_m/(self.distance/1000.0)
        return pace

    def show(self):
        print "Event: ", self.name, " Date: ", self.printDate()
        print "Distance: ",self.distance/1000.0,"KM, Time: ", self.printTime()
        print "Pace per 1 KM: ", self.getKmPace(), " minutes."

kdl = event("20KM",20000,[26,4,2014],[0,1,27,36])
kdl_bad = event("20KM",20000,[26,4,2013],[0,2,35,37])

print 'First event:'
kdl.show()

print 'Richard has two events:'
richard = competitor("Richard", 1993, 1, [kdl,kdl_bad])
richard.listEvents()

答案 1 :(得分:1)

我不认为这是继承的情况。在此示例中应用继承将类似于:

class event(object): # not inheriting from competitor
    # your code for the event
    #     ...

class 20KM_event(event):
    def __init__(self, date, time):
        super(20KM_event,self).__init__("20KM",20000, date, time)
    # if any specific methods are required for JUST the
    # 20KM event, put them here. Otherwise you're done

kdl = 20KM_event([26,4,2014],[0,1,27,36])

对于competitor,通常这是由event处理的问题。毕竟,他们是活动的成员,所以可能是这样的:

class event(object):
    def __init__(self,name,distance,date,time):
        self.name = name
        self.distance = distance
        self.date = date
        self.time = time
        self.competitors = []
    def addCompetitor(self,*args):
        """Add a competitor to this event
USAGE: self.addCompetitor(competitor) OR
       self.addCompetitor(name, dob, number, events)"""
        if len(args) == 1 and isinstance(args[0],competitor):
            target = args[0]
        else:
            target = competitor(*args)
        target.events.append(self)
        self.competitors.append(target)