派生类使用Base类的属性?

时间:2012-02-21 21:43:11

标签: c# class

我有一个带有公共属性“appController”的类,如下所示:

public class FAST
{
    #region Props

    public AppController.AppControllerClass appController = new AppController.AppControllerClass();

    #endregion

    #region Contructors

    public FAST(AppController.AppControllerClass appcontroller)
    {
        this.appController = appcontroller;
    }

    #endregion
}

我有另外几个课程,其中我想使用FAST的appController,上面的课程。他们看起来像:

public  class Forecast 
{
    #region Properties

    private int _forecastnumber;

    public int ForecastNumber
    {
        get { return _forecastnumber; }
        set { _forecastnumber = value; }
    }

    private DateTime _startdate;

    public DateTime StartDate
    {
        get { return _startdate; }
        set { _startdate = value; }
    }

    private DateTime _enddate;

    public DateTime EndDate
    {
        get { return _enddate; }
        set { _enddate = value; }
    }

    private DateTime _deadline;

    public DateTime Deadline
    {
        get { return _deadline; }
        set { _deadline = value; }
    }

    private string _name;

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    private string _type;

    public string Type
    {
        get { return _type; }
        set { _type = value; }
    }

    private string _description;

    public string Description
    {
        get { return _description; }
        set { _description = value; }
    }

    private string _status;

    public string Status
    {
        get { return _status; }
        set { _status = value; }
    }

    #endregion

    #region Constructors

    public Forecast()
    {

    }
    #endregion

    #region Methods

    public static void InsertForecast(Forecast forecast)
    {
        try
        {
            this.appController.Execute(appController.nDC.FASTData.InsertForecast(forecast.StartDate, forecast.EndDate, forecast.Deadline, forecast.Type, forecast.Name, forecast.Description, forecast.Status));
        }
        catch (Exception ex)
        {
            this.appController.LogError(ex);
        }
    }

    #endregion
}

我希望能够声明FAST类一次,传入AppController,然后自由使用我的其他类,他们将使用FAST类的appcontroller。

这可以完成吗? (继承?)

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

听起来你只想要一个FAST类的静态类。如果将AppController变量定义为static,则可以从任何地方访问它。

答案 1 :(得分:1)

我会拒绝继承。继承表明“是”关系,例如“预测是应用控制器的专用版本。”聚合是一种特殊形式的对象组合,它表示“有”关系,例如“预测有一个app控制器。”

http://en.wikipedia.org/wiki/Object_composition#Aggregation

您可以添加setter方法将FAST对象设置为Forecast的属性:

public FAST appController { get; set; }

然后

var f = new FAST(new AppController.AppControllerClass());
var forecast = new Forecast();
var forecast2 = new Forecast();
forecast.appController = f;
forecast2.appController = f;