2016-11-13 7 views
0

Pythonを学習してプログラムを作成...私は継承に関するドキュメントとフォーラムを読んでいますが、実装しようとしている間にこれを回避しようとしています。誰かが私の理解を助けてくれますか?Python 2.7ユーザー入力から値を継承するクラスから値を継承する

私は、ユーザー定義データからクラスへの継承を理解しています。私はそれをうまく実装しました。しかし今、私は別のクラスを継承するクラスから、ユーザー定義のデータを継承したい、第二度の継承ですか?

私はいくつかの重い計算を使用するプログラムを構築しています。私は、ユーザーが定義する部分を持っています:angle、x0:x5、y0:y5。次に、ユーザー定義のデータをとり、新しいxとy点を計算するcalculate_xy_at_angleクラスがあります。

次に、私は新しい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

[OK]を、あなたはので、私は、ちょうどあなたがそれが今、あなたは疑い理由を取得します。あなたは問題

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がそれを行います。

関連する問題