简体   繁体   中英

Derived Class to use property of Base class?

I have a class, with a public property "appController", as follows:

public class FAST
{
    #region Props

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

    #endregion

    #region Contructors

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

    #endregion
}

I have another few class, in which I would like to use the appController of FAST, the above class.They look like:

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
}

I want to be able to declare the FAST class once, passing in the AppController, then use my other classes freely, and they will use the appcontroller of the FAST class.

Can this be done at all? (inheritance?)

Thanks for any help.

It sounds like you simply want a static class for your FAST class. If you define the AppController variable as static, it will be accessible from anywhere.

I would say no to inheritance. Inheritance suggests an "is" relationship, eg "Forecast is a specialized version of the app controller." Aggregation, a specialized form of object composition, suggests a "has" relationship, eg "Forecast has an app controller."

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

You could add a setter method to set your FAST object as a property of Forecast:

public FAST appController { get; set; }

And then

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM