2012-01-09 85 views
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纵观可能是我所需要的。

回答

2

因为

public HistoricalReportWrapper Report { get; protected set; } 

不是

public abstract IReport Report { get; protected set; } 
+0

错误的实现,抱歉。我错过了重写,并补充说,但仍然没有去。然而HistoricalReportWrapper实现了IReport--是否有办法做我想要的? – 2012-01-09 20:25:55

+0

为什么财产首先抽象?你说你只想向世界展示一个IReport属性。 – 2012-01-09 20:30:49

+0

我把它标记为抽象的,因为CormantChart是一个抽象类,并且没有能力自己设置它。因此,我认为这些实现将由派生类来处理?如果我不把它标记为抽象,那会有什么帮助? – 2012-01-09 20:45:16