我已经编写了NewsApi()类并创建了Cricket()的子类。以下是代码片段: Class NewsApi
class NewsApi(object):
def __init__(self, parameter, country):
self.parameter = parameter
self.country = country
self.top_headlines =
newsapi.get_top_headlines(q=self.parameter,country=self.country)
self.description = []
self.url = []
self.image_link = []
self.title = []
self.sources = []
for article in self.top_headlines['articles']:
self.description.append(article['description'])
self.url.append(article['url'])
self.image_link.append(article['urlToImage'])
self.title.append(article['title'])
self.sources.append(article['source']['name'])
所以我想创建一个继承自NewsApi()类的板球类。我也想在Cricket()类中添加NewsApi类的构造函数的特定功能。 因为我不想在Cricket()类构造函数中再次编写相同的代码。
class CricketNews(NewsApi):
get_every = newsapi.get_everything(q="Cricket",
from_parameter=yesterday_date,
to=today_date,
language='en',
sort_by='relevancy',
page=2)
答案 0 :(得分:1)
您可以使用Super方法来调用超类的方法。
class CricketNews(NewsApi):
def __init__(self, parameter, country):
super(CricketNews, self).__init__(parameter, country)
# your init code.
get_every = newsapi.get_everything(q="Cricket",
from_parameter=yesterday_date,
to=today_date,
language='en',
sort_by='relevancy',
page=2)