0
這一切都比我的意圖有點棘手。我正在使用HistoricalReportWrapper
類,因爲我通過API檢索了我的數據,這使得直接使用HistoricalReport實現IReport
是不現實的。在派生類中努力實現抽象屬性
public abstract class CormantChart : Chart
{
public abstract IReport Report { get; protected set; }
}
public abstract class HistoricalChart : CormantChart
{
public override HistoricalReportWrapper Report { get; protected set; }
public HistoricalChart(HistoricalChartData chartData) : base(chartData)
{
Report = GetHistoricalReport(chartData.ReportID);
}
protected HistoricalReportWrapper GetHistoricalReport(int reportID)
{
return SessionRepository.Instance.HistoricalReports.Find(historicalReport => int.Equals(historicalReport.ID, reportID));
}
}
public class HistoricalReportWrapper : IReport
{
public HistoricalReport inner;
public int ID
{
get { return inner.ID; }
set { inner.ID = value; }
}
public string Name
{
get { return inner.Name; }
set { inner.Name = value; }
}
public HistoricalReportWrapper(HistoricalReport obj)
{
inner = obj;
}
}
public interface IReport
{
string Name { get; set; }
int ID { get; set; }
}
的這裏的想法是,當我的HistoricalChart
類的內部工作,我需要能夠訪問HistoricalReport的特定屬性。然而,我的程序的其餘部分只需要訪問HistoricalReport的ID和名稱。因此,我想將IReport的財產展示給全世界,但是隨後將詳細信息保存在課程中。
就目前而言,繼承HistoricalChart
的所有類都會生成「不實現繼承的抽象成員」以及HistoricalChart
上的警告,表明我隱藏了CormantChart的報表。
什麼是正確的方式來聲明這個來實現我想要的?
感謝
編輯:哎呀,我錯過了一個覆蓋。現在,當我嘗試重寫CormantChart報告我收到:
'CableSolve.Web.Dashboard.Charting.Historical_Charts.HistoricalChart.Report': type must be 'CableSolve.Web.Dashboard.IReport' to match overridden member 'CableSolve.Web.Dashboard.Charting.CormantChart.Report' C
EDIT2:在C#: Overriding return types縱觀可能是我所需要的。
錯誤的實現,抱歉。我錯過了重寫,並補充說,但仍然沒有去。然而HistoricalReportWrapper實現了IReport--是否有辦法做我想要的? – 2012-01-09 20:25:55
爲什麼財產首先抽象?你說你只想向世界展示一個IReport屬性。 – 2012-01-09 20:30:49
我把它標記爲抽象的,因爲CormantChart是一個抽象類,並且沒有能力自己設置它。因此,我認爲這些實現將由派生類來處理?如果我不把它標記爲抽象,那會有什麼幫助? – 2012-01-09 20:45:16