2016-11-13 75 views
0

学习Python并制作一个程序...我已经阅读了关于继承的文档和论坛,但是在试图实现它的时候,我正努力地围绕这一点努力。有人能帮我理解吗?Python 2.7继承继承用户输入值的类的值

我明白,我认为,从用户定义的数据继承到一个类,我成功地实现了它。但是现在我想从继承用户定义数据的类继承另一个类,第二次继承?

我正在构建一个使用一些重数学的程序。我有一个用户将定义的部分:角度,x0:x5和y0:y5。然后我有一个类calculate_xy_at_angle,它接受用户定义的数据并计算新的x和y点。

然后我有另一个类将采取新的x和y点,并计算ax:fx和ay:fy为多项式方程。我的代码如下(我切出X2后的代码...因为它很长,你得到的图片)

我的问题是,我不知道如何从

class calculate_xy_at_angle 

值将他们的计算值传递给

class abcdef_of_x? 

而且一旦用户定义了数据,我该如何检索最后一个类的值?在某种意义上,我必须做些什么才能使管道开始工作?

### Placeholders for User defined data 

angle = 30 

x0 = 0 
x1 = 1 
x2 = 5 
x3 = 7 
x4 = 5 
x5 = 1 

y0 = 0 
y1 = 5 
y2 = 8 
y3 = 9 
y4 = 2 
y5 = 0 

class calculate_xy_atangle(object): 

def __init__(self,angle,x0,x1,x2,x3,x4,x5,y0,y1,y2,y3,y4,y5): # will be user defined data 
    self.angle = angle 

    self.x0 = x0 
    self.x1 = x1 
    self.x2 = x2 
    self.x3 = x3 
    self.x4 = x4 
    self.x5 = x5 

    self.y0 = y0 
    self.y1 = y1 
    self.y2 = y2 
    self.y3 = y3 
    self.y4 = y4 
    self.y5 = y5 

### x 

def x0_at_angle(self): 
    x_0 = (self.x0*math.cos(math.radians(self.angle)))-(self.y0*math.sin(math.radians(self.angle))) 
    print x_0 
    return x_0 

def x1_at_angle(self): 
    x_1 = (self.x1*math.cos(math.radians(self.angle)))-(self.y1*math.sin(math.radians(self.angle))) 
    print x_1 
    return x_1 

def x2_at_angle(self): 
    x_2 = (self.x2*math.cos(math.radians(self.angle)))-(self.y2*math.sin(math.radians(self.angle))) 
    print x_2 
    return x_2 

#### more code 

### y 

def y0_at_angle(self): 
    y_0 = (self.x0*math.sin(math.radians(self.angle)))+(self.y0*math.cos(math.radians(self.angle))) 
    print y_0 
    return y_0 

def y1_at_angle(self): 
    y_1 = (self.x1*math.sin(math.radians(self.angle)))+(self.y1*math.cos(math.radians(self.angle))) 
    print y_1 
    return y_1 

def y2_at_angle(self): 
    y_2 = (self.x2*math.sin(math.radians(self.angle)))+(self.y2*math.cos(math.radians(self.angle))) 
    print y_2 
    return y_2 

### more code 


class abcdef_of_x(calculate_xy_atangle): # should inherit values from previous class 

def __init__(self,.....???): # this is where I am stuck on how to initialize and define 
    self.x0 = x0 # ? 
    self.x1 = x1 # ? 
    self.x2 = x2 
    self.x3 = x3 
    self.x4 = x4 
    self.x5 = x5 

def ax(self): 
    ax = (-1*self.x0)+(5*self.x1)+(-10*self.x2)+(10*self.x3)+(-5*self.x4)+(self.x5) 

    print "ax =", ax 
    return ax 

def bx(self): 
    bx = (5*self.x0)+(-20*self.x1)+(30*self.x2)+(-20*self.x3)+(5*self.x4) 

    print "bx =", bx 
    return bx 

def cx(self): 
    cx = (-10*self.x0)+(30*self.x1)+(-30*self.x2)+(10*self.x3) 

    print "cx =", cx 
    return cx 

## more code 

class abcdef_of_y(object): # this should also inherit from first class 
def __init__(self, y0, y1, y2, y3, y4, y5): 
    self.y0 = y0 
    self.y1 = y1 
    self.y2 = y2 
    self.y3 = y3 
    self.y4 = y4 
    self.y5 = y5 

def ay(self): 
    ay = (-1 * self.y0) + (5 * self.y1) + (-10 * self.y2) + (10 * self.y3) + (-5 * self.y4) + (self.y5) 

    print "ay =", ay 
    return ay 

def by(self): 
    by = (5 * self.y0) + (-20 * self.y1) + (30 * self.y2) + (-20 * self.y3) + (5 * self.y4) 

    print "by =", by 
    return by 

### more code 

回答

0

好吧,你错过了一点点大的事情,所以我只会说出点你应该记住,足以为您解决问题

class claculate_xy_atangle(object): 
    def __init__(self, x, y): 
     self.x = x 
     self.y = y 

class abcdef_of_x(calculate_xy_atangle): 
    def __init__(self): 
     super().__init__(x, y) #call the parent class constructor which is prerogative of the child class 

    def get_x_y(): 
     return self.x, self.y 
if __name__ == "__main__": 
    child = abcdef_of_x(12, 10) 
    x, y = child.get_x_y() #This give the value 12, 10 

现在你会得到一个疑问,为什么不是超类calculate_xy_atangle不会调用super().____ init ____(),默认情况下,python会为你做,因为每个类都继承了超类对象。