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

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

问题描述

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

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);"DatabaseDataDealer"类中的部分.

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)

那么是的,它可以,并且您可以使用抽象类中的每个属性和方法,包括调用抽象方法.但是,调用此方法的人必须发送一个继承抽象类的类的实例,如下所示:

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天全站免登陆