是否可以将抽象类的对象作为参数传递给类的实例? [英] Is it possible to pass the abstract class's object as argument to an instance of a class?

查看:349
本文介绍了是否可以将抽象类的对象作为参数传递给类的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以将抽象类的属性(或函数)值传递给实例类参数?还是我必须创建一个私有成员变量,为它分配数据,然后将此变量传递给实例类参数?听起来令人困惑,所以下面的示例将有助于更好地进行说明。

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.

看看 new DealerAccountRepository(MyEnvironmentSetting);。

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...
}


推荐答案

如果您要问一个方法是否可以采用这样的抽象对象:

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

private void DoSomething(AEnvironmentSetting a)

然后可以,您可以使用抽象类中包含的每个属性和方法,包括调用抽象方法。
但是,无论谁调用此方法,都必须发送calss的实例,它会像这样继承抽象类:

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);

,如果将从 DoSomething 然后将调用 DatabaseDataDealer 的实现

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

这篇关于是否可以将抽象类的对象作为参数传递给类的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆