Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 派生类是否要使用基类的属性?_C#_Class - Fatal编程技术网

C# 派生类是否要使用基类的属性?

C# 派生类是否要使用基类的属性?,c#,class,C#,Class,我有一个类,具有公共属性“appController”,如下所示: public class FAST { #region Props public AppController.AppControllerClass appController = new AppController.AppControllerClass(); #endregion #region Contructors public FAST(AppController.AppCo

我有一个类,具有公共属性“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

这能做到吗?(继承权?)


谢谢你的帮助。

听起来你只是想要一个静态类来支持你的快速类。如果将AppController变量定义为静态变量,则可以从任何位置访问它。

我会拒绝继承。继承表示“是”关系,例如“预测是应用程序控制器的专用版本”。聚合是对象组合的专用形式,表示“有”关系,例如“预测有应用程序控制器”

您可以添加一个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;

谢谢我不喜欢这个方法的地方是每次使用forecast类或创建该类的实例时都需要设置appController。您考虑过依赖注入吗?有一些很好的.NET库,比如Ninject。