C#';类别.IMDb';不包含接受0个参数的构造函数
本文关键字:0个 参数 构造函数 类别 IMDb 包含接 | 更新日期: 2023-09-27 18:25:11
我有一个类,它从Imdb.com获取关于电影蚂蚁的信息,并将信息存储在类变量中。我有另外一个类,它在数据库中写入有关电影的信息。所以,我想进行继承,获取变量信息并保存到数据库中。但我得到错误:'FilmuBiblioteka.IMDb' does not contain a constructor that takes 0 arguments
这是我的代码:
public class IMDb
{
public bool status { get; set; }
public string Id { get; set; }
public string Title { get; set; }
public string OriginalTitle { get; set; }
public string Year { get; set; }
public string Rating { get; set; }
public ArrayList Genres { get; set; }
public ArrayList Directors { get; set; }
public ArrayList Cast { get; set; }
public string Plot { get; set; }
public string Poster { get; set; }
public string PosterLarge { get; set; }
public string PosterFull { get; set; }
public string Runtime { get; set; }
public ArrayList Languages { get; set; }
public ArrayList Countries { get; set; }
public string ImdbURL { get; set; }
//Search Engine URLs
private string GoogleSearch = "http://www.google.com/search?q=imdb+";
private string BingSearch = "http://www.bing.com/search?q=imdb+";
private string AskSearch = "http://www.ask.com/web?q=imdb+";
private Func<string> toString;
private bool v;
//Constructor
public IMDb(string MovieName, bool GetExtraInfo = true)
{
string imdbUrl = getIMDbUrl(System.Uri.EscapeUriString(MovieName));
status = false;
if (!string.IsNullOrEmpty(imdbUrl))
{
parseIMDbPage(imdbUrl, GetExtraInfo);
}
}
public IMDb(Func<string> toString, bool v)
{
this.toString = toString;
this.v = v;
}
//Get IMDb URL from search results
private string getIMDbUrl(string MovieName, string searchEngine = "google")
{
string url = GoogleSearch + MovieName; //default to Google search
if (searchEngine.ToLower().Equals("bing")) url = BingSearch + MovieName;
if (searchEngine.ToLower().Equals("ask")) url = AskSearch + MovieName;
string html = getUrlData(url);
ArrayList imdbUrls = matchAll(@"<a href=""(http://www.imdb.com/title/tt'd{7}/)"".*?>.*?</a>", html);
if (imdbUrls.Count > 0)
return (string)imdbUrls[0]; //return first IMDb result
else if (searchEngine.ToLower().Equals("google")) //if Google search fails
return getIMDbUrl(MovieName, "bing"); //search using Bing
else if (searchEngine.ToLower().Equals("bing")) //if Bing search fails
return getIMDbUrl(MovieName, "ask"); //search using Ask
else //search fails
return string.Empty;
}
//Parse IMDb page data
private void parseIMDbPage(string imdbUrl, bool GetExtraInfo)
{
string html = getUrlData(imdbUrl + "combined");
Id = match(@"<link rel=""canonical"" href=""http://www.imdb.com/title/(tt'd{7})/combined"" />", html);
if (!string.IsNullOrEmpty(Id))
{
status = true;
Title = match(@"<title>(IMDb '- )*(.*?) '(.*?</title>", html, 2);
OriginalTitle = match(@"title-extra"">(.*?)<", html);
Year = match(@"<title>.*?'(.*?('d{4}).*?').*?</title>", html);
Rating = match(@"<b>('d.'d)/10</b>", html);
Genres = matchAll(@"<a.*?>(.*?)</a>", match(@"Genre.?:(.*?)(</div>|See more)", html));
Directors = matchAll(@"<td valign=""top""><a.*?href=""/name/.*?/"">(.*?)</a>", match(@"Directed by</a></h5>(.*?)</table>", html));
Cast = matchAll(@"<td class=""nm""><a.*?href=""/name/.*?/"".*?>(.*?)</a>", match(@"<h3>Cast</h3>(.*?)</table>", html));
Plot = match(@"Plot:</h5>.*?<div class=""info-content"">(.*?)(<a|</div)", html);
Runtime = match(@"Runtime:</h5><div class=""info-content"">('d{1,4}) min['s]*.*?</div>", html);
Languages = matchAll(@"<a.*?>(.*?)</a>", match(@"Language.?:(.*?)(</div>|>.?and )", html));
Countries = matchAll(@"<a.*?>(.*?)</a>", match(@"Country:(.*?)(</div>|>.?and )", html));
Poster = match(@"<div class=""photo"">.*?<a name=""poster"".*?><img.*?src=""(.*?)"".*?</div>", html);
if (!string.IsNullOrEmpty(Poster) && Poster.IndexOf("media-imdb.com") > 0)
{
Poster = Regex.Replace(Poster, @"_V1.*?.jpg", "_V1._SY200.jpg");
PosterLarge = Regex.Replace(Poster, @"_V1.*?.jpg", "_V1._SY500.jpg");
PosterFull = Regex.Replace(Poster, @"_V1.*?.jpg", "_V1._SY0.jpg");
}
else
{
Poster = string.Empty;
PosterLarge = string.Empty;
PosterFull = string.Empty;
}
ImdbURL = "http://www.imdb.com/title/" + Id + "/";
if (GetExtraInfo)
{
string plotHtml = getUrlData(imdbUrl + "plotsummary");
}
}
}
/*******************************[ Helper Methods ]********************************/
//Match single instance
private string match(string regex, string html, int i = 1)
{
return new Regex(regex, RegexOptions.Multiline).Match(html).Groups[i].Value.Trim();
}
//Match all instances and return as ArrayList
private ArrayList matchAll(string regex, string html, int i = 1)
{
ArrayList list = new ArrayList();
foreach (Match m in new Regex(regex, RegexOptions.Multiline).Matches(html))
list.Add(m.Groups[i].Value.Trim());
return list;
}
//Strip HTML Tags
static string StripHTML(string inputString)
{
return Regex.Replace(inputString, @"<.*?>", string.Empty);
}
//Get URL Data
private string getUrlData(string url)
{
WebClient client = new WebClient();
Random r = new Random();
//Random IP Address
client.Headers["X-Forwarded-For"] = r.Next(0, 255) + "." + r.Next(0, 255) + "." + r.Next(0, 255) + "." + r.Next(0, 255);
//Random User-Agent
client.Headers["User-Agent"] = "Mozilla/" + r.Next(3, 5) + ".0 (Windows NT " + r.Next(3, 5) + "." + r.Next(0, 2) + "; rv:2.0.1) Gecko/20100101 Firefox/" + r.Next(3, 5) + "." + r.Next(0, 5) + "." + r.Next(0, 5);
Stream datastream = client.OpenRead(url);
StreamReader reader = new StreamReader(datastream);
StringBuilder sb = new StringBuilder();
while (!reader.EndOfStream)
sb.Append(reader.ReadLine());
return sb.ToString();
}
}
public class filmai : IMDb
{
public System.Data.DataSet gauticonnection
{
get
{ return gauti(); }
}
private System.Data.DataSet gauti()
{
System.Data.SqlClient.SqlConnection sqlConnection1;
System.Data.SqlClient.SqlDataAdapter da;
sqlConnection1 = new System.Data.SqlClient.SqlConnection(@"Data Source=(LocalDB)'v11.0;AttachDbFilename=C:'Users'Darius'Documents'Visual Studio 2013'Projects'FilmuBiblioteka'FilmuBiblioteka'duombaze.mdf");
da = new SqlDataAdapter("select * from filmai", sqlConnection1);
DataSet ds = new DataSet();
da.Fill(ds, "Table");
try
{
sqlConnection1.Open();
da.Fill(ds);
}
catch (Exception e)
{
throw;
}
finally
{
if (sqlConnection1.State == System.Data.ConnectionState.Open)
sqlConnection1.Close();
}
return ds;
}
public void prideti()
{
System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection(@"Data Source=(LocalDB)'v11.0;AttachDbFilename=C:'Users'Darius'Documents'Visual Studio 2013'Projects'FilmuBiblioteka'FilmuBiblioteka'duombaze.mdf");
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT filmai (Id, Pavadinimas, Metai, trukme, salis, kalba, reitingas, zanras, aktoriai, link, plot, rezisieriai) VALUES (Id, Title, Year, Runtime, Countries, Languages, Rating, Genres, Cast, ImdbURL, Plot, Directors)";
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
}
}
我在这一行得到错误:public class filmai : IMDb
如前所述,您遇到的编译错误是因为filmai
类的当前构造函数没有正确调用IMDb
类的构造函数。
要正确地从基类继承,您必须在派生类的已实现构造函数运行之前调用它的构造函数,例如:
public filmai(string filmName)
: base(filmName, false)
{
}
这将使用提供给filmai
构造函数的影片名称调用public IMDb(string MovieName, bool GetExtraInfo = true)
的base
构造函数,并表示它不需要额外的信息。
我不确定你的用例,因为看起来filmai
纯粹用于数据库访问,而IMDb
用于请求一部电影?但是在filmai
上实现构造函数并调用: base(...)
将解决您现在的问题。