Is it possible to pass the abstract class's object as argument to an instance of a class?

做~自己de王妃 提交于 2019-12-11 03:07:47

问题


I'm wondering is is it possible to pass on abstract class's property (or function) values a instance class argument? Or do I have to create a private member variable, assign data to it and pass this one to the instance class argument instead? That's sounds confusing, so the example below will help to clarify it better.

Take a look at the "new DealerAccountRepository(MyEnvironmentSetting);" section in the "DatabaseDataDealer" class.

public abstract class AEnvironmentSetting
{
    //Constructor...
    protected AEnvironmentSetting(EnvironmentSetting parmEnvironmentSetting)
    {
        _environmentSetting = new EnvironmentSetting
        {
            Emulation = parmEnvironmentSetting.Emulation, 
            Database = parmEnvironmentSetting.Database
        };
    }

    //Member variables...
    private EnvironmentSetting _environmentSetting = null;

    //Get/Set properties...
    protected string MyEmulation { get { return _environmentSetting.Emulation; } }  //No need for "set {}" property...
    protected string MyDatabase { get { return _environmentSetting.Database; } }  //No need for "set {}" property...

    //Functions...
    protected EnvironmentSetting MyEnvironmentSetting()
    {
        return _environmentSetting;
    }
}

public class DealerAccountRepository : AEnvironmentSetting
{
    //Constructor...
    public DealerAccountRepository(EnvironmentSetting parmEnvironmentSetting) : base(parmEnvironmentSetting)
    {
    }

    //Functions...
    public string Foo_Emulation()
    {
        return MyEmulation;  //This object coming from the abstract class "AEnvironmentSetting"...
    }
    public string Foo_Database()
    {
        return MyDatabase;  //This object coming from the abstract class "AEnvironmentSetting"...
    }
    public EnvironmentSetting Foo_EnvironmentSetting()
    {
        return MyEnvironmentSetting();  //This object coming from the abstract class "AEnvironmentSetting"...
    }
}

public class DatabaseDataDealer : AEnvironmentSetting
{
    //Constructor...
    public DatabaseDataDealer(EnvironmentSetting parmEnvironmentSetting) : base(parmEnvironmentSetting)
    {
    }

    //Get/Set properties...
    public DealerAccountRepository DealerAccount { get { return new DealerAccountRepository(MyEnvironmentSetting); } }  //No need for "set {}" property...

    //Functions...
    //N/A...
}

回答1:


if you are asking if a method can take an abstract object like this:

private void DoSomething(AEnvironmentSetting a)

then yes, it can, and you can use every property and method there is in the abstract class including calling abstract method. however, whoever calls this method will have to send an instance of a calss inherits the abstract class like so:

DatabaseDataDealer d = new DatabaseDataDealer ();
DoSomething(d);

and if an abstract method will be call from DoSomething then implementation of DatabaseDataDealer will be called

in the constructor you can call the base and you will get to the abstract constructor. in the end let me just say the best way to learn this is to try. compile and run your code and put a break point to see how it goes and through which steps



来源:https://stackoverflow.com/questions/17911285/is-it-possible-to-pass-the-abstract-classs-object-as-argument-to-an-instance-of

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!