Python 2.7从一个继承用户输入值的类继承值

时间:2016-11-13 05:44:01

标签: python python-2.7 inheritance

学习Python并制作一个程序......我已经阅读了有关继承的文档和论坛,但在尝试实现它时,我正在努力解决这个问题。有人可以帮我理解吗?

我理解,我认为,从用户定义的数据继承到一个类,我成功实现了它。但是现在我想让另一个类继承自继承用户定义数据的类,继承二级继承?

我正在构建一个使用大量数学的程序。我有一个用户将定义的部分:angle,x0:x5和y0:y5。然后我有一个compute_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

1 个答案:

答案 0 :(得分: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()。_ _ _ _ _ _ _ _ _ _(),默认情况下python为你做,因为每个类都继承了超类对象。