2015-04-22 44 views
0

玩弄繼承,並遇到一個錯誤,指出我輸入了太多的參數。我可能做錯了什麼?對象繼承arugment輸入錯誤

第一個文件叫做media.py

class Video(): 
    def __init__(self, title, duration): 
     self.title = title 
     self.duration = duration 

class Movie(Video): 
    def __init__(self, movie_story, movie_poster, trailer_youtube): 
     Video.__init__(self, title, duration) 
     self.storyline = movie_story 
     self.poster_image_url = movie_poster 
     self.trailer_youtube_url = trailer_youtube 

    def show_trailer(self): 
     webbrowser.open(self.trailer_youtube_url) 


class TvShow(Video): 
    def __init__(self, season, episode, tv_station): 
     Video.__init__(self, title, duration) 
     self.season = season 
     self.episode = episode 
     self.tv_station = tv_station 

這第二個文件創建的對象。

import fresh_tomatoes 
import media 



family_guy = media.TvShow("Family Guy", 
          "2000-Present", 
          "Fifteen Seasons", 
          "Twenty-Eight", 
          "Fox") 


print family_guy.title 

終端輸出狀態我傳遞6個參數時,只有4可能被接受。這是爲什麼?

+0

你看過你傳遞的參數並將它們與你定義的構造函數進行比較嗎? – user2357112

+0

@ user2357112很好,因爲視頻被TvShow繼承,不應該按照參數的順序:標題,持續時間,季節,劇集,tv_station? – JCD

+0

否。重寫的構造函數不會自動繼承重寫表單的參數。 – user2357112

回答

0

調用父項__init__只會調用它,但您仍然需要將參數傳遞給它。

因此,當您調用TvShow的__init__方法時,它只會期望3 + 1(自)參數,而您試圖發送更多的參數。所以要解決這個問題,您只需增加__init__除外的參數數量即可。

class Video(): 
    def __init__(self, title, duration): 
     self.title = title 
     self.duration = duration 

class Movie(Video): 
    def __init__(self, movie_story, movie_poster, trailer_youtube): 
     Video.__init__(self, title, duration) 
     self.storyline = movie_story 
     self.poster_image_url = movie_poster 
     self.trailer_youtube_url = trailer_youtube 

    def show_trailer(self): 
     webbrowser.open(self.trailer_youtube_url) 


class TvShow(Video): 
    def __init__(self, season, episode, tv_station, title, duration): 
     Video.__init__(self, title, duration) 
     self.season = season 
     self.episode = episode 
     self.tv_station = tv_station 
+0

明白了。謝謝你,先生。 – JCD